php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #79717 Inconsistent handling of 5-digit years
Submitted: 2020-06-19 16:49 UTC Modified: 2022-07-22 16:13 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:0 (0.0%)
From: a dot schilder at gmx dot de Assigned: derick (profile)
Status: Closed Package: Date/time related
PHP Version: 7.4.7 OS:
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: a dot schilder at gmx dot de
New email:
PHP Version: OS:

 

 [2020-06-19 16:49 UTC] 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

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2021-10-01 17:31 UTC] cmb@php.net
-Status: Open +Status: Verified
 [2021-10-01 17:31 UTC] cmb@php.net
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.
 [2022-07-22 16:13 UTC] derick@php.net
-Status: Verified +Status: Closed -Assigned To: +Assigned To: derick
 [2022-07-22 16:13 UTC] derick@php.net
The fix for this bug has been committed.
If you are still experiencing this bug, try to check out latest source from https://github.com/php/php-src and re-test.
Thank you for the report, and for helping us make PHP better.

This will be possible to do with the new X (and x) format letters that we've added to PHP 8.2.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Dec 03 07:01:33 2024 UTC