PHP strtotime returning false

冷暖自知 提交于 2019-12-14 03:22:40

问题


Below is the output from a database and I am trying to get the number of months between the start date and the date from the database. Below is my output

string(10) "12/12/2010" Date : 12/12/2010 - 1292112000
string(10) "19/04/2000" Date : 19/04/2000 -
string(10) "08/08/2007" Date : 08/08/2007 - 1186527600
string(10) "20/06/2011" Date : 20/06/2011 - 

As you can see, the second and fourth unix timestamps are empty. I don't know what could be causing the issue. Below is the code I'm using to echo what you see above.

echo "Date : " . $row['startDate'] . " - " . strtotime($row['startDate']) . var_dump($row['startDate']). "<br />";

回答1:


From the manual:

Dates in the m/d/y or d-m-y formats are disambiguated by looking at the separator between the various components: if the separator is a slash (/), then the American m/d/y is assumed; whereas if the separator is a dash (-) or a dot (.), then the European d-m-y format is assumed.

To avoid potential ambiguity, it's best to use ISO 8601 (YYYY-MM-DD) dates or DateTime::createFromFormat() when possible.




回答2:


Solved the issue, I used some of the new DateTime functions in 5.3 PHP

$originalDate = str_replace("/", "-", $row['startDate']);
$date1 = new DateTime($originalDate);
$date2 = new DateTime('now');

$interval = date_diff($date1, $date2);

echo "Months" . $interval->format('%m') . ($interval->format('%y') * 12) . "<br />";


来源:https://stackoverflow.com/questions/15687822/php-strtotime-returning-false

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!