php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #75992 DateTime::createFromFormat() does not work with years > 4 digits
Submitted: 2018-02-22 10:23 UTC Modified: 2018-02-22 11:11 UTC
From: dktapps at pmmp dot io Assigned:
Status: Not a bug Package: Date/time related
PHP Version: 7.2.2 OS: Windows 10
Private report: No CVE-ID: None
 [2018-02-22 10:23 UTC] dktapps at pmmp dot io
Description:
------------
DateTime formats can be created using format() which have >4 digits in the year, which cannot be parsed using createFromFormat() with the same format-string. As seen below the same format is used both times.

This bug report looks absurd, but I've had similar issues been auto-reported to my crash archive when users put absurd values in configuration, and this appears to be why.

Test script:
---------------
<?php

$datetime = new \DateTime('@' . PHP_INT_MAX);

$str = $datetime->format("Y-m-d H:i:s O");
var_dump($str); //275808-11-04 10:06:43 +0000

var_dump(\DateTime::createFromFormat("Y-m-d H:i:s O", $str)); //bool(false)

?>

Expected result:
----------------
Either:

bool(false)
bool(false)

OR

string(33) "292277026596-12-04 15:30:07 +0000
object(DateTime)#2 (3) {
  ["date"]=>
  string(34) "292277026596-12-04 15:30:07.000000
  ["timezone_type"]=>
  int(1)
  ["timezone"]=>
  string(6) "+00:00"
}

Actual result:
--------------
string(33) "292277026596-12-04 15:30:07 +0000"
bool(false)

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2018-02-22 11:11 UTC] pmmaga@php.net
-Summary: DateTime::createFromString() does not work with years > 4 digits +Summary: DateTime::createFromFormat() does not work with years > 4 digits -Status: Open +Status: Not a bug
 [2018-02-22 11:11 UTC] pmmaga@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

The docs for DateTime::createFromFormat() clearly state:
> Y	A full numeric representation of a year, 4 digits

The fact that this works as you expect for output doesn't imply that it should work for input.
 [2018-02-22 14:22 UTC] dktapps at pmmp dot io
My expectation was that this should either fail for both, or fail for neither.
Since the "Y" format character expects 4 digits, it appears problematic from my perspective that format() can emit dates with more than 4 digits having used "Y" in the first place.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Nov 26 22:01:33 2024 UTC