DateTime :: change and set the DST switch

Using DateTime :: modify to add an hour across the DST border, it skips the hour.

eg.

$d = new DateTime('2015-11-01 12:00:00 AM', new DateTimeZone('America/Vancouver')); $d->modify('+1 hour'); // 1 AM $d->modify('+1 hour'); // 2 AM $d->modify('+1 hour'); // 3 AM 

I want to see “1 AM” twice (and then “2 AM”) because the time returns in an hour.

How can I get this behavior?

+5
source share
1 answer

This is a mistake . ( credit )

To work around this, change the time zone to UTC, and then again.

 $d = new DateTime('2015-11-01 12:00:00 AM', new DateTimeZone('America/Vancouver')); $tz = getTimezone(); $d->setTimezone(new DateTimeZone('UTC')); $d->modify('+1 hour'); $d->modify('+1 hour'); $d->modify('+1 hour'); $d->setTimezone($tz); echo $d->format('dMY g:ia'); // 01-Nov-2015 2:00am 
+1
source

All Articles