1

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
4

2 回答 2

3
  1. 错误的原因是Mon, 21 May 2013我的日历中 没有什么像21st MayTuesday

  2. 来自 PHP 文档

m/d/y 或 dmy 格式的日期通过查看各个组件之间的分隔符来消除歧义:如果分隔符是斜杠 (/),则假定为美式 m/d/y;而如果分隔符是破折号 (-) 或点 (.),则假定为欧洲 dmy 格式。

为避免潜在的歧义,最好尽可能使用 ISO 8601 (YYYY-MM-DD) 日期或 DateTime::createFromFormat()。

例子

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

输出

2013-05-21 11:10:30  <- strtotime
2013-05-21 09:10:30  <- datetime 
于 2013-05-20T21:36:39.590 回答
1

根据php.net,这种格式实际上可能是无效的;这里有一个可接受的格式列表:http ://www.php.net/manual/en/datetime.formats.php - 没有提到星期几。

于 2013-05-20T21:28:16.423 回答