PHP strtotime错误的日期与GMT转换? [英] PHP strtotime giving wrong date with GMT conversion?

查看:356
本文介绍了PHP strtotime错误的日期与GMT转换?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读一个Last-Modified标题,作为一个字符串是Mon,21 May 2013 09:10:30 GMT,并尝试将其与当地时间()(新西兰)进行比较。但是我刚刚注意到,当字符串中包含Mon时,strtotime将日期设为27而不是21。是正常吗我做错了吗?认为我错过了一些...

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. 错误的原因是没有什么像 2013年5月21日星期一从我的日历 21 st 可以 code>

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

从PHP DOC




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

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.

为避免潜在的模糊性,最好使用ISO 8601(YYYY-MM-DD)日期或DateTime :: createFromFormat()。

To avoid potential ambiguity, it's best to use ISO 8601 (YYYY-MM-DD) dates or DateTime::createFromFormat() when possible.

示例

$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 

这篇关于PHP strtotime错误的日期与GMT转换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆