问题
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