1

I'm using php 8. I have a DateTimeImmutable object in timezone "Europe/Zurich"

$start = new DateTimeImmutable('2021-03-28 01:00', new DateTimeZone("Europe/Zurich")); 
// Europe/Zurich is in UTC+1 during winter, UTC+2 in summer

Then I add 1, resp. 2 hours using DateInterval

$oneHour = $start->add(new DateInterval('PT1H'));
$twoHour = $start->add(new DateInterval('PT2H'));
echo $oneHour->format('c');
echo $twoHour->format('c');

What i expect:

"2021-03-28T03:00:00+02:00"
"2021-03-28T04:00:00+02:00"

What i get:

"2021-03-28T03:00:00+02:00" 
"2021-03-28T03:00:00+02:00" <-- same result!

Is this a php bug? Why does it fail? Is it expected behaviour?

simon.ro
  • 2,984
  • 2
  • 22
  • 36
  • 3
    Take a look at [similar question](https://stackoverflow.com/questions/43761145/php-datetime-add-gets-wrong-on-dst), the answer suggests using `UTC` for time operations and later using required timezone for display. – biesior Mar 29 '21 at 12:44
  • 1
    And indeed it seems to be a bug, also read this issue at Github - some answer get a list of tracked PHP issues about it: https://github.com/briannesbitt/Carbon/issues/1951 – biesior Mar 29 '21 at 12:52
  • Thanks for pointing @biesior! Doing all calculations in UTC might be safer, but thats simply not satisfactory given the fact that phps DateTime pretends to be able to handle timezones. – simon.ro Mar 29 '21 at 13:27

0 Answers0