简体   繁体   English

PHP strtotime返回false

[英]PHP strtotime returning false

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. 如您所见,第二个和第四个Unix时间戳是空的。 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 />";

From the manual : 手册

Dates in the m/d/y or dmy 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; 通过查看各个组成部分之间的分隔符,可以消除m / d / y或dmy格式的日期的歧义:如果分隔符为斜杠(/),则假定为美国m / d / y; whereas if the separator is a dash (-) or a dot (.), then the European dmy format is assumed. 相反,如果分隔符是破折号(-)或点(。),则采用欧洲dmy格式。

To avoid potential ambiguity, it's best to use ISO 8601 (YYYY-MM-DD) dates or DateTime::createFromFormat() when possible. 为避免潜在的歧义,最好尽可能使用ISO 8601(YYYY-MM-DD)日期或DateTime :: createFromFormat()。

Solved the issue, I used some of the new DateTime functions in 5.3 PHP 解决了这个问题,我在5.3 PHP中使用了一些新的DateTime函数

$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 />";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM