php / php/php-src

Miscalculation of a date using microseconds

Đang mở
#20,550 1 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

The following code:

<?php
date_default_timezone_set("UTC");

$beginningOfYear = new \DateTimeImmutable('1st january this year')
    ->setTime(0, 0);
$endOfYear = new \DateTimeImmutable('31th december this year')
    ->setTime(23, 59, 59, 999999);


$uuThisYear = (int) $endOfYear->format('Uu') - (int) $beginningOfYear->format('Uu');
$sThisYear = (int) $endOfYear->format('U') - (int) $beginningOfYear->format('U');
$usThisYear = (int) $endOfYear->format('u') - (int) $beginningOfYear->format('u');

$uuThisYear2 = (int) ($sThisYear . $usThisYear);

echo "<pre>" . var_export([
    'from' => $beginningOfYear,
    'fromMs' => (int) $beginningOfYear->format('Uu'),
    'to' => $endOfYear,
    'toMs' => (int) $endOfYear->format('Uu'),
    'nbMicrosecondsBetweenToAndFrom' => $uuThisYear,
    'nbMicrosecondsBetweenToAndFromOtherCalculation' => $uuThisYear2,
    'equal' => $uuThisYear === $uuThisYear2,
    'nbSecondsBetweenToAndFrom' => $sThisYear,
    'nbUsecondsBetweenToAndFrom' => $usThisYear,
    'shouldEqualToFromButDoesntWork' => (clone $endOfYear)->modify('-' . $uuThisYear . ' µsec'),
    'shouldEqualToFromWorks' => (clone $endOfYear)
        ->modify('-' . $sThisYear . ' seconds')
        ->modify('-' . $usThisYear . ' microseconds'),
], true);

Resulted in this output:

array (
  'from' => 
  \DateTimeImmutable::__set_state(array(
     'date' => '2025-01-01 00:00:00.000000',
     'timezone_type' => 3,
     'timezone' => 'UTC',
  )),
  'fromMs' => 1735689600000000,
  'to' => 
  \DateTimeImmutable::__set_state(array(
     'date' => '2025-12-31 23:59:59.999999',
     'timezone_type' => 3,
     'timezone' => 'UTC',
  )),
  'toMs' => 1767225599999999,
  'nbMicrosecondsBetweenToAndFrom' => 31535999999999,
  'nbMicrosecondsBetweenToAndFromOtherCalculation' => 31535999999999,
  'equal' => true,
  'nbSecondsBetweenToAndFrom' => 31535999,
  'nbUsecondsBetweenToAndFrom' => 999999,
  'shouldEqualToFromButDoesntWork' => 
  \DateTimeImmutable::__set_state(array(
     'date' => '2026-01-01 09:59:59.999998',
     'timezone_type' => 3,
     'timezone' => 'UTC',
  )),
  'shouldEqualToFromWorks' => 
  \DateTimeImmutable::__set_state(array(
     'date' => '2025-01-01 00:00:00.000000',
     'timezone_type' => 3,
     'timezone' => 'UTC',
  )),
)

But I expected this output instead:

array (
  'from' => 
  \DateTimeImmutable::__set_state(array(
     'date' => '2025-01-01 00:00:00.000000',
     'timezone_type' => 3,
     'timezone' => 'UTC',
  )),
  'fromMs' => 1735689600000000,
  'to' => 
  \DateTimeImmutable::__set_state(array(
     'date' => '2025-12-31 23:59:59.999999',
     'timezone_type' => 3,
     'timezone' => 'UTC',
  )),
  'toMs' => 1767225599999999,
  'nbMicrosecondsBetweenToAndFrom' => 31535999999999,
  'nbMicrosecondsBetweenToAndFromOtherCalculation' => 31535999999999,
  'equal' => true,
  'nbSecondsBetweenToAndFrom' => 31535999,
  'nbUsecondsBetweenToAndFrom' => 999999,
  'shouldEqualToFromButDoesntWork' => 
  \DateTimeImmutable::__set_state(array(
     'date' => '2025-01-01 00:00:00.000000',
     'timezone_type' => 3,
     'timezone' => 'UTC',
  )),
  'shouldEqualToFromWorks' => 
  \DateTimeImmutable::__set_state(array(
     'date' => '2025-01-01 00:00:00.000000',
     'timezone_type' => 3,
     'timezone' => 'UTC',
  )),
)

Check the "shouldEqualToFromButDoesntWork" value.

PHP Version
8.4.14
Operating System

Mint 22.2 Zara

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

Start by reproducing the DateTimeImmutable::modify('-... µsec') case from the issue on PHP 8.4.14, then trace the date/time implementation that handles microsecond modifiers. Compare it with the separate seconds-and-microseconds calls; done when the single microsecond subtraction produces the same 2025-01-01 00:00:00.000000 result.

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
Đặc tả rõ ràng
Mức phù hợp với người mới
42/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.