php / php/php-src

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

Đang mở
#15,880 0 bình luận 0 reaction 1 người được giao Xem trên GitHub

@derickr đang làm issue này rồi.

Từ ngày 13/9/2024.

Bug Extension: date Status: Needs Triage
Ngôn ngữ chính
C
Star
40.4k
Fork
8.1k
Merge trung bình
2 ngày 13 giờ
Pull request đã merge (30 ngày)
96

Mô tả

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

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Đánh giá

Issue này chưa được đánh giá.

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.