问题
I'm reading a Last-Modified header which as a string is "Mon, 21 May 2013 09:10:30 GMT" and trying to compare that to my local time() (New Zealand). But I've just noticed that strtotime is making the date the "27" instead of "21" when "Mon, " is included in the string. Is that normal? Am I doing something wrong? Think I'm missing something...
$strtotime = strtotime("Mon, 21 May 2013 09:10:30 GMT");
$strtotime_date = date("Y-m-d H:i:s",$strtotime);
//[strtotime] => 1369645830
//[strtotime_date] => 2013-05-27 21:10:30
回答1:
The reason for the error is that there is nothing like
Mon, 21 May 2013from my calendar21stMayisTuesdayFrom PHP DOC
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.
Examples
$strtotime = strtotime("Tue, 21 May 2013 09:10:30 GMT");
echo date("Y-m-d H:i:s", $strtotime),PHP_EOL;
$strtotime = DateTime::createFromFormat("D, d M Y g:i:s O", "Tue, 21 May 2013 09:10:30 GMT");
echo $strtotime->format("Y-m-d H:i:s");
Output
2013-05-21 11:10:30 <- strtotime
2013-05-21 09:10:30 <- datetime
回答2:
According to php.net, this format may actually be invalid; there's a list of acceptable formats here: http://www.php.net/manual/en/datetime.formats.php - day of the week is not mentioned.
来源:https://stackoverflow.com/questions/16658640/php-strtotime-giving-wrong-date-with-gmt-conversion