php / php/php-src

Wrong result from DateTimeImmutable::diff

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

Chưa có ai nhận issue này.

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

Mô tả

Description

If you execute this code, it produces an extremely weird result. The first DTI is 11 minutes after what we’re ->diff() ’ing against, but when in the America/Los_Angeles TZ, it produces a very strange result. Converting the first DTI to UTC then produces the expected difference of 11 minutes.

//Difference of -1 year, 11 months, 31 days, etc.
<?php
$invalid_result = (new \DateTimeImmutable('2024-01-31 17:00:00', new \DateTimeZone('America/Los_Angeles')))
    ->diff(new \DateTimeImmutable('2024-02-01 00:49:00'));

//Difference of 11 minutes
$correct_result = (new \DateTimeImmutable('2024-01-31 17:00:00', new \DateTimeZone('America/Los_Angeles')))
    ->setTimezone(new \DateTimeZone('UTC'))
    ->diff(new \DateTimeImmutable('2024-02-01 00:49:00'));

var_dump($invalid_result);
var_dump($correct_result);

$invalid_result Resulted in this output:

object(DateInterval)#3 (10) {
  ["y"]=>
  int(-1)
  ["m"]=>
  int(11)
  ["d"]=>
  int(31)
  ["h"]=>
  int(0)
  ["i"]=>
  int(11)
  ["s"]=>
  int(0)
  ["f"]=>
  float(0)
  ["invert"]=>
  int(1)
  ["days"]=>
  int(0)
  ["from_string"]=>
  bool(false)
}

But I expected this output instead:

object(DateInterval)#2 (10) {
  ["y"]=>
  int(0)
  ["m"]=>
  int(0)
  ["d"]=>
  int(0)
  ["h"]=>
  int(0)
  ["i"]=>
  int(11)
  ["s"]=>
  int(0)
  ["f"]=>
  float(0)
  ["invert"]=>
  int(1)
  ["days"]=>
  int(0)
  ["from_string"]=>
  bool(false)
}
PHP Version

PHP 8.1

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.

Hướng nghiên cứu

Bắt đầu bằng cách tái hiện script PHP 8.1 được cung cấp và so sánh DateTimeImmutable::diff khi có và không có setTimezone('UTC'). Theo dõi điểm vào của DateTimeImmutable::diff và cách xử lý múi giờ, sau đó thêm coverage cho thấy trường hợp America/Los_Angeles tạo ra khoảng thời gian 11 phút như mong đợi.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
php
Lĩnh vực
backend
Loại issue
Lỗi
Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Khá rõ ràng
Mức phù hợp với người mới
35/100

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.