Bug #79717 [Opn->Ver]: Inconsistent handling of 5-digit years

From: Date: Fri, 01 Oct 2021 17:31:29 +0000
Subject: Bug #79717 [Opn->Ver]: Inconsistent handling of 5-digit years
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-236979@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=79717&edit=1 ID: 79717 Updated by: cmb@php.net Reported by: a dot schilder at gmx dot de Summary: Inconsistent handling of 5-digit years -Status: Open +Status: Verified Type: Bug Package: Date/time related PHP Version: 7.4.7 Block user comment: N Private report: N New Comment: Confirmed: <https://3v4l.org/1bl8m> > Variant C (5-digit years not supported): This might be most reasonable, given that date/time calculations that far into the future are likely inaccurate anyway. Previous Comments: ------------------------------------------------------------------------ [2020-06-19 16:49:32] a dot schilder at gmx dot de Description: ------------ When adding intervals to a date time, this can result in a 5-digit year. When formating this date time, the format "Y" (which should output a 4 digit year following the documentation) outputs a 5-digit year - but it's not possible to use this output again as input, because then the year is limited to 4 digits again. Tested with PHP 7.4.7 and 8.0-dev Test script: --------------- <?php $datetime1 = new \DateTimeImmutable('9999-01-02 15:00:00.000000', new \DateTimeZone('UTC')); try { $datetime2 = $datetime1->add(new \DateInterval('P1Y')); echo $datetime2->format('Y-m-d H:i:s.u') . \PHP_EOL; } catch (\Throwable $e) { echo 'exception' . \PHP_EOL; } $datetime3 = \DateTimeImmutable::createFromFormat('Y-m-d H:i:s.u', $datetime2->format('Y-m-d H:i:s.u'), new \DateTimeZone('UTC')); if ($datetime3 !== false) { echo $datetime3->format('Y-m-d H:i:s.u') . \PHP_EOL; } else { echo 'false' . \PHP_EOL; } try { $datetime4 = new \DateTimeImmutable($datetime2->format('Y-m-d H:i:s.u'), new \DateTimeZone('UTC')); echo $datetime4->format('Y-m-d H:i:s.u') . \PHP_EOL; } catch (\Throwable $e) { echo 'exception' . \PHP_EOL; } // Workaround with fake year try { $datetime5 = new \DateTimeImmutable($datetime2->format('1234-m-d H:i:s.u'), new \DateTimeZone('UTC')); $datetime5 = $datetime5->setDate((int)$datetime2->format("Y"), (int)$datetime2->format("n"), (int)$datetime2->format("j")); echo $datetime5->format('Y-m-d H:i:s.u') . \PHP_EOL; } catch (\Throwable $e) { echo 'exception' . \PHP_EOL; } Expected result: ---------------- Variant A ("Y" handled as 4+ digits, preferred): 10000-01-02 15:00:00.000000 10000-01-02 15:00:00.000000 10000-01-02 15:00:00.000000 10000-01-02 15:00:00.000000 Variant B ("Y" handled as 4 digits, following the behavior of "y" - if the support for the year zero in PHP is the expected behavior and not another bug): 0000-01-02 15:00:00.000000 0000-01-02 15:00:00.000000 0000-01-02 15:00:00.000000 0000-01-02 15:00:00.000000 Variant C (5-digit years not supported): exception false exception exception Actual result: -------------- 10000-01-02 15:00:00.000000 false exception 10000-01-02 15:00:00.000000 ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=79717&edit=1

« previous php.bugs (#236979) next »