[php-src] Issue #20550: Miscalculation of a date using microseconds

From: Date: Fri, 21 Nov 2025 18:21:24 +0000
Subject: [php-src] Issue #20550: Miscalculation of a date using microseconds
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-251270@lists.php.net to get a copy of this message
Issue: https://github.com/php/php-src/issues/20550 Author: axi ### Description The following code: ```php <?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 ```plain 8.4.14 ``` ### Operating System Mint 22.2 Zara

« previous php.bugs (#251270) next »