Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DateTimeImmutable (and DateTime) modify does not respect Daylight Saving Time shifts #15880

Open
karptonite opened this issue Sep 13, 2024 · 0 comments

Comments

@karptonite
Copy link

karptonite commented Sep 13, 2024

Description

The following code:

<?php
date_default_timezone_set("America/Chicago");

$startDate = new DateTimeImmutable( "2024-11-01" );
var_dump( $startDate );
$endDate = $startDate->modify( '+72 hour' );
var_dump($endDate);
$otherEndDate = $startDate->add( new DateInterval( 'PT72H' ) );
var_dump( $otherEndDate );

Resulted in this output:

object(DateTimeImmutable)#1 (3) {
  ["date"]=>
  string(26) "2024-11-01 00:00:00.000000"
  ["timezone_type"]=>
  int(3)
  ["timezone"]=>
  string(15) "America/Chicago"
}
object(DateTimeImmutable)#2 (3) {
  ["date"]=>
  string(26) "2024-11-04 00:00:00.000000"
  ["timezone_type"]=>
  int(3)
  ["timezone"]=>
  string(15) "America/Chicago"
}
object(DateTimeImmutable)#4 (3) {
  ["date"]=>
  string(26) "2024-11-03 23:00:00.000000"
  ["timezone_type"]=>
  int(3)
  ["timezone"]=>
  string(15) "America/Chicago"
}

But I expected this output instead:

object(DateTimeImmutable)#1 (3) {
  ["date"]=>
  string(26) "2024-11-01 00:00:00.000000"
  ["timezone_type"]=>
  int(3)
  ["timezone"]=>
  string(15) "America/Chicago"
}
object(DateTimeImmutable)#4 (3) {
  ["date"]=>
  string(26) "2024-11-03 23:00:00.000000"
  ["timezone_type"]=>
  int(3)
  ["timezone"]=>
  string(15) "America/Chicago"
}
object(DateTimeImmutable)#4 (3) {
  ["date"]=>
  string(26) "2024-11-03 23:00:00.000000"
  ["timezone_type"]=>
  int(3)
  ["timezone"]=>
  string(15) "America/Chicago"
}

Note that the from 11-1 to 11-4, Daylight Saving time ends. If you change the time by adding a DateInterval, it gets it right, but it gets it wrong with modify. Also note that if instead of adding 72 hours, you add 3 days, the inconsistency goes away--DateInterval understands, apparently, that one of those days is 25 hours, and adjusts correctly (at least, it seems correct to me) to Midnight on 11-4.

PHP Version

PHP 8.3

Operating System

No response

@karptonite karptonite changed the title DateTimeImmutable (and DateTime) Modify does not respect Daylight Saving Time shifts DateTimeImmutable (and DateTime) modify does not respect Daylight Saving Time shifts Sep 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants