简体   繁体   English

PHP日期中的奇怪行为

[英]strange behaviour in php date

A simple code to print a date 一个简单的代码来打印日期

$first_date = 1319493600;
$last_date = 1320184800;

$testDate = $first_date;


while ($testDate<=$last_date)
{
    echo strftime("%a", $testDate).' '. strftime("%d", $testDate).' '.strftime("%b", $testDate).'<br>';
    $testDate += 86400;
}

Yes this simple script fails me as you see below 是的,这个简单的脚本使我失望,如下所示

Tue 25 Oct
Wed 26 Oct
Thu 27 Oct
Fri 28 Oct
Sat 29 Oct
Sun 30 Oct <----Error
Sun 30 Oct <----Error
Mon 31 Oct
Tue 01 Nov

Anyone has any idea why? 有人知道为什么吗?

Looks like it's related to DST .. clocks are changed on the last sunday of october, in this year Sun 30 Oct. 看起来与DST有关。.时钟在10月的最后一个星期日,即今年10月30日,星期日更改。

Note: you also have to be in the "right" timezone for this to happen, eg here it's: 注意:您还必须处于“正确的”时区才能发生这种情况,例如,这里是:

 # date -ud@1319493600
 Mon Oct 24 22:00:00 UTC 2011

so that one hour doesn't make any difference, but for you it does. 这样一小时没有任何区别,但对您而言却有所不同。 This can be quite annoying as people won't be able to reproduce your problem :) 这可能很烦人,因为人们将无法重现您的问题:)

This is, in fact, the reason why you should never increment the day by adding 24 hours to the timestamp. 实际上,这就是为什么您不应该通过在时间戳上增加24小时来增加一天的原因。 Chances are that you are in a timezone that observes daylight saving time and the last Sunday of October is not 24 hours long. 可能您所在的时区遵守夏令时,而十月的最后一个星期日不是24小时长。

You can confirm this by adding echo strftime("%r %x", $testDate); 您可以通过添加echo strftime("%r %x", $testDate);来确认这一点echo strftime("%r %x", $testDate); inside the loop. 在循环内。 (Also note that you can use multiple modifiers in strftime() so you don't have to concatenate them: strftime("%a %d %b", $testDate); .) (还要注意,您可以在strftime()使用多个修饰符,因此不必将它们连接起来: strftime("%a %d %b", $testDate);

Always use date functions like strtotime() or mktime() to increment the date. 始终使用日期函数(如strtotime()mktime()增加日期。

This probably has to do with the daylight savings time change on 30 Oct. Try running the script with the times printed out too to see the change. 这可能与10月30日的夏令时更改有关。尝试运行打印出时间的脚本也可以查看更改。 Here's an example in my timezone: 这是我所在时区的一个示例:

php > $first_date = 1319493600;
php > $last_date = 1320184800;
php >
php > $testDate = $first_date;
php >
php >
php > while ($testDate<=$last_date)
php > {
php {     echo strftime("%a", $testDate).' '. strftime("%d", $testDate).' '.strftime("%b", $testDate). '(' . strftime("%d.%m.%Y %H:%M:%S", $testDate) . ")\n";
php {     $testDate += 86400;
php { }
Mon 24 Oct(24.10.2011 23:00:00)
Tue 25 Oct(25.10.2011 23:00:00)
Wed 26 Oct(26.10.2011 23:00:00)
Thu 27 Oct(27.10.2011 23:00:00)
Fri 28 Oct(28.10.2011 23:00:00)
Sat 29 Oct(29.10.2011 23:00:00)
Sun 30 Oct(30.10.2011 22:00:00)
Mon 31 Oct(31.10.2011 22:00:00)
Tue 01 Nov(01.11.2011 22:00:00)

On my machine the output is: 在我的机器上,输出为:

root@ubuntu:~# php /tmp/tst.php
Mon 24 Oct
Tue 25 Oct
Wed 26 Oct
Thu 27 Oct
Fri 28 Oct
Sat 29 Oct
Sun 30 Oct
Mon 31 Oct
Tue 01 Nov
root@ubuntu:~# php -v
PHP 5.3.5-1ubuntu7.2 with Suhosin-Patch (cli) (built: May  2 2011 23:18:30)
Copyright (c) 1997-2009 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2010 Zend Technologies

what PHP version are you using? 您正在使用哪个PHP版本?

Also, it might have something to do with daylight savings - try printing the hours too 此外,它可能与夏时制有关-也尝试打印小时数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM