php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #66836 DateTime::createFromFormat 'U' with pre 1970 dates fails parsing
Submitted: 2014-03-06 14:29 UTC Modified: 2016-07-11 14:12 UTC
Votes:2
Avg. Score:3.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:0 (0.0%)
From: rami dot alnawas at gmail dot com Assigned: cmb (profile)
Status: Closed Package: Date/time related
PHP Version: Irrelevant OS: Ubuntu
Private report: No CVE-ID: None
 [2014-03-06 14:29 UTC] rami dot alnawas at gmail dot com
Description:
------------
When DateTime::createFromFormat('U', strtotime('1962-11-17T00:00:00+00:00') is executed the result is false as strtotime('1962-11-17T00:00:00+00:00') is negative.

Using date function with the same input works OK

Test script:
---------------
echo date('YmdHis', strtotime('1962-11-17T00:00:00+00:00'));  // 19621117000000


print_r(DateTime::createFromFormat('U', strtotime('1962-11-17T00:00:00+00:00'), new \DateTimeZone('UTC'))->format('YmdHis'));   // fails as createFromFormat returns false



Patches

Pull Requests

Pull requests:

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2016-07-11 12:35 UTC] cmb@php.net
-Summary: DateTime::createFromFormat 'U' with pre 1970 dates, fails parsing +Summary: DateTime::createFromFormat 'U' with pre 1970 dates fails parsing -Status: Open +Status: Verified -Assigned To: +Assigned To: cmb
 [2016-07-11 12:35 UTC] cmb@php.net
I can confirm the issue, see <https://3v4l.org/mT3CX>. However, it
has recently been fixed in master[1], and might be backported.

[1] <https://github.com/php/php-src/commit/822ca45220db0b864af9dc79b94bda03a9323621>
 [2016-07-11 12:52 UTC] derick@php.net
I've pushed it to 5.6 and up - there is no need for a further back port.
 [2016-07-11 13:11 UTC] cmb@php.net
I can confirm that the issue is solved in PHP-7.0 and master, but
not in PHP-5.6, as the following PHPT fails there:

    --TEST--
    Bug #66836 (DateTime::createFromFormat 'U' with pre 1970 dates fails parsing)
    --FILE--
    <?php
    $timestamp = '-1';
    $dt = DateTime::createFromFormat('U', $timestamp);
    var_dump($dt->format('U') === $timestamp);
    ?>
    --EXPECT--
    bool(true)
 [2016-07-11 13:49 UTC] derick@php.net
@cmb, I can't reproduce that:

[GIT: PHP-5.6][PHP: 5.6.20-dev  ]
derick@whisky:~/dev/php/php-src.git $ php
    <?php
    $timestamp = '-1';
    $dt = DateTime::createFromFormat('U', $timestamp);
var_dump($dt, $dt->format('U'), $timestamp);
?>

But fixed in .25-dev:

[GIT: PHP-5.6][PHP: 5.6.25-dev  ]
derick@whisky:~/dev/php/php-src.git $ php
    <?php
    $timestamp = '-1';
    $dt = DateTime::createFromFormat('U', $timestamp);
var_dump($dt, $dt->format('U'), $timestamp);
?>
    -:4:
class DateTime#1 (3) {
  public $date =>
  string(26) "1969-12-31 23:59:59.000000"
  public $timezone_type =>
  int(1)
  public $timezone =>
  string(6) "+00:00"
}
-:4:
string(2) "-1"
-:4:
string(2) "-1"
 [2016-07-11 13:49 UTC] cmb@php.net
The test fails because of the date.timezone warning. So everything
is okay. Sorry for the noise.
 [2016-07-11 14:12 UTC] cmb@php.net
-Status: Verified +Status: Closed
 [2016-07-11 14:12 UTC] cmb@php.net
I have added a regression test and the NEWS entry:
<http://git.php.net/?p=php-src.git;a=commit;h=966311e6274e59d0eabdccf5bf9c5d554c90202c>.
 [2016-07-28 09:37 UTC] howard at zedcore dot com
This bug fix was not mentioned in the 5.6.24 change logs, could it be added please?
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 12:01:31 2024 UTC