strtotime 日期奇怪的结果

发布于 2024-12-02 18:18:11 字数 375 浏览 1 评论 0原文

给定以下字符串日期: 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

贵在坚持 2024-12-09 18:18:11

我认为你误会了,
在这种情况下没有夏令时,
但是GMT,由于

我的时区 (GMT+8)

php -r "echo date('r', strtotime('Fri Sep 02 2011 21:00:00 GMT+0100'));"
Sat, 03 Sep 2011 04:00:00 +0800

,您获得了 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)

php -r "echo date('r', strtotime('Fri Sep 02 2011 21:00:00 GMT+0100'));"
Sat, 03 Sep 2011 04:00:00 +0800

which I gain 7 hours, due to GMT+8 - GMT+1 = 7

薄凉少年不暖心 2024-12-09 18:18:11

我意识到这与夏令时有关,但如何弥补这一点?

不使用 date()strtotime();首选 DateTime 类。

$str_date = 'Fri Sep 02 2011 21:00:00 GMT+0100';
$datetime = new DateTime($str_date);
echo $datetime->format('c'); // 2011-09-02T21:00:00+01:00

或以程序风格

$str_date = 'Fri Sep 02 2011 21:00:00 GMT+0100';
echo date_format(date_create($str_date), 'c'); // 2011-09-02T21:00:00+01:00

旁白:如果您仍然希望使用 date()/strtotime() 那么,正如其他答案和您自己的观察结果所示,您需要小心以及日期字符串和脚本中使用的时区。

I realise this is to do with daylight savings, but how does one compensate for this?

By not using date() and strtotime(); the DateTime class is preferred.

$str_date = 'Fri Sep 02 2011 21:00:00 GMT+0100';
$datetime = new DateTime($str_date);
echo $datetime->format('c'); // 2011-09-02T21:00:00+01:00

or in procedural style

$str_date = 'Fri Sep 02 2011 21:00:00 GMT+0100';
echo date_format(date_create($str_date), 'c'); // 2011-09-02T21:00:00+01:00

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.

白芷 2024-12-09 18:18:11

您使用哪个 PHP 版本?您的date.timezone 设置是什么?我这样问是因为我无法在 Mac OS X 上重现运行 PHP 5.3.6 的输出:

$str_date   = 'Fri Sep 02 2011 21:00:00 GMT+0100  (GMT Daylight Time)';
echo $str_date,"  Vs ",date("c",strtotime($str_date));
// Fri Sep 02 2011 21:00:00 GMT+0100  (GMT Daylight Time)  Vs 1970-01-01T01:00:00+01:00

这是正确的,因为 Fri Sep 02 2011 21:00:00 GMT+0100 (GMT Daylight Time) 不是有效的日期/时间字符串。

$str_date   = 'Fri Sep 02 2011 21:00:00 GMT+0100';
echo $str_date,"  Vs ",date("c",strtotime($str_date));
// Fri Sep 02 2011 21:00:00 GMT+0100  Vs 2011-09-02T22:00:00+02:00

这是正确的,因为我现在处于 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:

$str_date   = 'Fri Sep 02 2011 21:00:00 GMT+0100  (GMT Daylight Time)';
echo $str_date,"  Vs ",date("c",strtotime($str_date));
// Fri Sep 02 2011 21:00:00 GMT+0100  (GMT Daylight Time)  Vs 1970-01-01T01:00:00+01:00

This is correct because Fri Sep 02 2011 21:00:00 GMT+0100 (GMT Daylight Time) is not a valid date/time string.

$str_date   = 'Fri Sep 02 2011 21:00:00 GMT+0100';
echo $str_date,"  Vs ",date("c",strtotime($str_date));
// Fri Sep 02 2011 21:00:00 GMT+0100  Vs 2011-09-02T22:00:00+02:00

This is correct because I'm in GMT+2.

你的呼吸 2024-12-09 18:18:11

似乎 strtotime() 将您的时间呈现为 SOAP 格式:YY "-" MM "-" DD "T" HH ":" II ":" SS frac tz Correction?

结果是:

"2008-07-01T22:35:17.02", "2008-07-01T22:35:17.03+08:00"

您可以尝试将时间字符串格式化为其他时间格式。查看 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:

"2008-07-01T22:35:17.02", "2008-07-01T22:35:17.03+08:00"

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文