strtotime 日期奇怪的结果
给定以下字符串日期: Fri Sep 02 2011 21:00:00 GMT+0100 (GMT Daylight Time)
in php 如果我在上面执行 strtotime,然后将其转换回字符串日期,好像多了一个小时。
echo $str_date," Vs ",date("c",strtotime($str_date));
生成:
Fri Sep 02 2011 21:00:00 GMT+0100(GMT 夏令时间)
与 2011-09-02T22:00:00+01:00
我意识到这一点与夏令时有关,但如何弥补这一点呢?
Given the following string date: Fri Sep 02 2011 21:00:00 GMT+0100 (GMT Daylight Time)
in php if I do a strtotime on the above, and then convert it back to a string date, it seems to gain an hour.
echo $str_date," Vs ",date("c",strtotime($str_date));
Produces:
Fri Sep 02 2011 21:00:00 GMT+0100 (GMT Daylight Time)
Vs 2011-09-02T22:00:00+01:00
I realise this is to do with daylight savings, but how does one compensate for this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我认为你误会了,
在这种情况下没有夏令时,
但是GMT,由于
我的时区 (GMT+8)
,您获得了 1 小时,而由于
GMT+8 - GMT+1 = 7
,我获得了 7 小时I think you misunderstanding,
there is not day light saving in this case,
BUT GMT, you gain one hour because of that
in my timezone (GMT+8)
which I gain 7 hours, due to
GMT+8 - GMT+1 = 7
不使用
date()
和strtotime()
;首选DateTime
类。或以程序风格
旁白:如果您仍然希望使用
date()
/strtotime()
那么,正如其他答案和您自己的观察结果所示,您需要小心以及日期字符串和脚本中使用的时区。By not using
date()
andstrtotime()
; theDateTime
class is preferred.or in procedural style
Aside: if you wish to still use
date()
/strtotime()
then, as the other answers and your own observations show, you need to be careful with the time zones in use in the date string and your script.您使用哪个 PHP 版本?您的
date.timezone
设置是什么?我这样问是因为我无法在 Mac OS X 上重现运行 PHP 5.3.6 的输出:这是正确的,因为
Fri Sep 02 2011 21:00:00 GMT+0100 (GMT Daylight Time)
不是有效的日期/时间字符串。这是正确的,因为我现在处于 GMT+2。
Which PHP version do you use? What is your
date.timezone
setting? I'm asking because I cannot reproduce your output running PHP 5.3.6 on Mac OS X:This is correct because
Fri Sep 02 2011 21:00:00 GMT+0100 (GMT Daylight Time)
is not a valid date/time string.This is correct because I'm in GMT+2.
似乎
strtotime()
将您的时间呈现为 SOAP 格式:YY "-" MM "-" DD "T" HH ":" II ":" SS frac tz Correction?
结果是:
您可以尝试将时间字符串格式化为其他时间格式。查看 http://www.php.net/manual/en/datetime .formats.compound.php
Seems like
strtotime()
renders your time as SOAP format:YY "-" MM "-" DD "T" HH ":" II ":" SS frac tzcorrection?
result is:
You can try to format your time string as some other time format. Look in http://www.php.net/manual/en/datetime.formats.compound.php