gpt4 book ai didi

php - PHP 中的日期格式 m/d/y 为 Y-m-d?

转载 作者:行者123 更新时间:2023-11-29 13:44:25 25 4
gpt4 key购买 nike

我正在尝试格式化一个从网上获取的日期字符串,日期为 m/d/y,我需要将其插入 MYSQL 当前我收到错误 PHP Fatal error: Call to a member function format( )在非对象上

代码:

<?php
include 'ganon.php';
$id = array(8573, 53816, 7746, 80748, 7714);

for($l=0; $l<sizeof($id); $l++) {
$html = file_get_dom("http://pregame.com/pregamepros/pro-bettor/picks.aspx?id=" . $id[$l]);

$picks = $html('div[class="div-table-col"]');

$array = array();
$j =0;
for($i=0; $i<sizeof($picks); $i+=8) {
$array[$j] = array("date" => trim($picks[$i]->getPlainText()),
"sport" => trim($picks[$i+1]->getPlainText()),
"pick" => trim($picks[$i+2]->getPlainText()),
"score" => trim($picks[$i+3]->getPlainText()),
"odds" => trim($picks[$i+4]->getPlainText()),
"size" => preg_replace('/\$/', "", $picks[$i+5]->getPlainText()),
"winloss" => trim($picks[$i+6]->getPlainText()),
"money" => (int)preg_replace('/\$/', "", $picks[$i+7]->getPlainText()));
$j++;
}

//enter picks into database
//make sure we do not add picks we already have
$mysqli = new mysqli("host", "user", "pass", "db");
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
exit();
}
if($id[$l] == 8573) {
//$query = "SELECT `date` FROM `db`.`vegasrunner` where date=" . date('Y-m-d');
for($i=0; $i<sizeof($array); $i++) {
$query = "SELECT `date`,`pick` FROM `db`.`vegasrunner` where date=" . "'" . $array[$i]["date"] . "'" . " AND pick=" . "'" . $array[$i]["pick"] . "'";
$result = $mysqli->query($query);
$row = $result->fetch_row();
if(sizeof($row) < 1) {
$result->close();
$date = new DateTime();
$date = DateTime::createFromFormat('m/d/y', $array[$i]["date"]);
//$date = $array[$i]["date"];
$sport = $array[$i]["sport"];
$pick = $array[$i]["pick"];
$score = $array[$i]["score"];
$odds = $array[$i]["odds"];
$size = $array[$i]["size"];
$winloss = $array[$i]["winloss"];
$money = $array[$i]["money"];

echo $date->format('Y-m-d');

$query = "INSERT INTO `db`.`vegasrunner` (`date`, `sport`, `pick`, `score`, `odds`, `size`, `winloss`, `money`) VALUES (" . "'" . $date->format('Y-m-d') . "'" . ", '$sport', '$pick', '$score', '$odds', '$size', '$winloss', '$money')";

$mysqli->query($query);
}
} }

最佳答案

我能看到的唯一合理的解释是 createFromFormat() 是否失败,如果输入日期不是您期望的格式,则可能会发生这种情况。

检查输入字符串是否采用您认为的格式,并更改代码以在 createFromFormat() 调用中包含失败检查。

关于php - PHP 中的日期格式 m/d/y 为 Y-m-d?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17624098/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com