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: -
From: a dot schilder at gmx dot de Assigned:
Status: Open Package: Date/time related
PHP Version: 7.4.7 OS:
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
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

Add a Patch

Pull Requests

Add a Pull Request

 
PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Wed Oct 28 03:01:23 2020 UTC