Fixed PHP DateTime :: format ('I') error?
I am wondering if this is a bug, or if I am missing something.
When creating a method to allocate a timestamp when DST switches from on to off and vice versa, I noticed a potential error in the DateTime :: ("I") format. When the DST ends and the time jumps back one hour, the DateTime :: ("I") format reports the jump one hour too late.
In America / Montreal timezone, we can see these timestamps when DST ends:
1414904400, Sun, 02 November 2014 01:00:00 -0400 (DST on)
1414908000, Sun, 02 November 2014 01:00:00 -0500 (deferred from service)
1414911600, Sun, 02 November 2014 02: 00:00 -0500 (DST off)
Note that DST ends at the second timestamp when 01:00:00 repeats.
But, using DateTime :: ('I') format, the ending is indicated at the third timestamp.
$timezone = new DateTimezone('America/Montreal');
$datetime = new DateTime('now', $timezone);
$datetime->setTimestamp(1414904400);
echo $datetime->format('I');
$datetime->setTimestamp(1414908000);
echo $datetime->format('I');
$datetime->setTimestamp(1414911600);
echo $datetime->format('I');
returns 110 (should be 100)
If we use the date ('I'), we get the correct answer.
date_default_timezone_set('America/Montreal');
echo date('I', 1414904400);
echo date('I', 1414908000);
echo date('I', 1414911600);
returns 100 (as expected)
When DST starts DateTime :: format ('I') works fine, it only seems to fail when DST ends.
Also, I've tested this for several different years in several different time zones, but I don't know the depth of this potential bug. Much more testing.
Does anyone know what this is?
source to share
When creating a method to allocate a timestamp when DST switches from on to off and vice versa ...
This method already exists as DateTimeZone::GetTransitions
. The documentation is here.
You may also be interested in the related issue of determining if a particular date / time is in the transition period, which is described in this answer .
source to share