我发现DateTime
和DateInterval
个对象出现了一种非常奇怪的行为,并想知道为什么它的行为方式。
将DateInterval
1天的DateTime
添加到午夜设置的DateTime
会导致<?php
$timezone1 = 'Atlantic/Azores';
$timezone2 = 'America/Chicago';
function testZone($tz) {
printf("*** time zone: %s ***\n", $tz);
date_default_timezone_set($tz);
$oneday = DateInterval::createFromDateString('1 day');
$day = new DateTime('2016-03-26 00:00:00');
printf("Before:\t\t\t%s\n", $day->format('Y-m-d H:i:s'));
$day->add($oneday);
printf("After adding one day:\t%s\n", $day->format('Y-m-d H:i:s'));
$day = new DateTime('2016-03-25 00:00:00');
printf("Before:\t\t\t%s\n", $day->format('Y-m-d H:i:s'));
$day->add($oneday);
printf("After adding one day:\t%s\n", $day->format('Y-m-d H:i:s'));
}
testZone($timezone1);
testZone($timezone2);
设置为凌晨1:00 ...但仅限某些日期,且仅限某些日期时区。
*** time zone: Atlantic/Azores ***
Before: 2016-03-26 00:00:00
After adding one day: 2016-03-27 01:00:00 <~~ 1 AM!
Before: 2016-03-25 00:00:00
After adding one day: 2016-03-26 00:00:00
*** time zone: America/Chicago ***
Before: 2016-03-26 00:00:00
After adding one day: 2016-03-27 00:00:00
Before: 2016-03-25 00:00:00
After adding one day: 2016-03-26 00:00:00
输出:
kelime
似乎是this has always been a problem in PHP。
这对我没有任何意义。将整个日期间隔添加到整个日期时,为什么还会增加一个小时?
我知道我可以解决它,但有一些设置我可以调整来解决这个问题吗?
答案 0 :(得分:0)
这种情况正在发生,因为葡萄牙语夏令时正好在2016-03-27
开始。
如果你懂葡萄牙语,你可以阅读:
http://oal.ul.pt/hora-legal/mudanca-da-hora/
RegiãoAutónomadosAçores
Em conformidade comaminitlação,a hora legalnaRegiãoAutónomados Açores酒店:
seráadiantada60minutosàs0horas de tempo legal(1 hora UTC)do dia 27deMarçoeatrasada 60minutosà1hora de tempo legal(1 hora UTC)do dia 30 de Outubro。