在PHP m/d/y到y -m-d格式日期?

时间:2021-10-20 16:57:52

I am trying to format a date string i rip from the web the date comes in as m/d/y and I need to insert it into MYSQL currently I get an error PHP Fatal error: Call to a member function format() on a non-object

我正在尝试格式化一个日期字符串,我从web上删除日期以m/d/y的形式出现,我需要把它插入到MYSQL中,现在我得到一个错误PHP致命错误:调用一个非对象的成员函数格式()。

Code:

代码:

<?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);
}
    } }  

2 个解决方案

#1


1  

The only plausible explanation I can see is if createFromFormat() is failing, which might happen if the input date isn't in the format you're expecting.

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

Check that the input string is in the format you think, and alter your code to include a check for failure at the createFromFormat() call.

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

#2


0  

I ended up writing my own function to parse the date. It turns out there was a hidden space before the month.

最后,我编写了自己的函数来解析日期。在这个月之前有一个隐藏的空间。

function formatDate($date) {
    //date = 07/12/13
    $date = explode('/', $date);
    //for some reason in ubuntu month had a space had to get last 2 characters
    $month = substr($date[0], -2);
    $day = trim($date[1]);
    $year = date('y') == $date[2] ? date('Y') : date('Y');

    return $year . "-" . $month . "-" . $day;
}

#1


1  

The only plausible explanation I can see is if createFromFormat() is failing, which might happen if the input date isn't in the format you're expecting.

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

Check that the input string is in the format you think, and alter your code to include a check for failure at the createFromFormat() call.

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

#2


0  

I ended up writing my own function to parse the date. It turns out there was a hidden space before the month.

最后,我编写了自己的函数来解析日期。在这个月之前有一个隐藏的空间。

function formatDate($date) {
    //date = 07/12/13
    $date = explode('/', $date);
    //for some reason in ubuntu month had a space had to get last 2 characters
    $month = substr($date[0], -2);
    $day = trim($date[1]);
    $year = date('y') == $date[2] ? date('Y') : date('Y');

    return $year . "-" . $month . "-" . $day;
}