PHP strtotime giving wrong date with GMT conversion? [closed]

穿精又带淫゛_ 提交于 2019-12-11 07:12:28

问题


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:


  1. The reason for the error is that there is nothing like Mon, 21 May 2013 from my calendar 21stMay is Tuesday

  2. From 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

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