php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #78499 [7.2.0+] DateTime on certain dates
Submitted: 2019-09-05 22:06 UTC Modified: 2019-09-05 22:12 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: alexanderpas at gmail dot com Assigned:
Status: Duplicate Package: Date/time related
PHP Version: 7.4.0RC1 OS: Any
Private report: No CVE-ID: None
 [2019-09-05 22:06 UTC] alexanderpas at gmail dot com
Description:
------------
When creating a DateTime object using a unix timestamp, the resulting DateTime object does not represent the same timestamp as it was created with.


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

$tests[] = '0000-01-28 00:00:00 UTC';
$tests[] = '0000-01-29 00:00:00 UTC';
$tests[] = '0000-01-30 00:00:00 UTC';
$tests[] = '0000-01-31 00:00:00 UTC';
$tests[] = '0000-02-01 00:00:00 UTC';

$tests[] = '0000-02-27 00:00:00 UTC';
$tests[] = '0000-02-28 00:00:00 UTC';
$tests[] = '0000-02-29 00:00:00 UTC';
$tests[] = '0000-02-30 00:00:00 UTC'; // intentionally invalid day, should be the same as the next.
$tests[] = '0000-03-01 00:00:00 UTC';
$tests[] = '0000-03-02 00:00:00 UTC';

foreach ($tests as $test)
{
    $timestamp1 = strtotime($test);
    $date_time = new DateTime('@' . $timestamp1);
    $timestamp2 = $date_time->getTimestamp();
    print $timestamp1 . "\n";
    print $timestamp2 . "\n";
    if ($timestamp1 != $timestamp2)
    {
        print "timestamps are not equal\n";
    }
    print_r($date_time);
    print "\n";
}


Expected result:
----------------
-62164886400
-62164886400
DateTime Object
(
    [date] => 0000-01-28 00:00:00.000000
    [timezone_type] => 1
    [timezone] => +00:00
)

-62164800000
-62164800000
DateTime Object
(
    [date] => 0000-01-29 00:00:00.000000
    [timezone_type] => 1
    [timezone] => +00:00
)

-62164713600
-62164713600
DateTime Object
(
    [date] => 0000-01-30 00:00:00.000000
    [timezone_type] => 1
    [timezone] => +00:00
)

-62164627200
-62164627200
DateTime Object
(
    [date] => 0000-01-31 00:00:00.000000
    [timezone_type] => 1
    [timezone] => +00:00
)

-62164540800
-62164540800
DateTime Object
(
    [date] => 0000-02-01 00:00:00.000000
    [timezone_type] => 1
    [timezone] => +00:00
)

-62162294400
-62162294400
DateTime Object
(
    [date] => 0000-02-27 00:00:00.000000
    [timezone_type] => 1
    [timezone] => +00:00
)

-62162208000
-62162208000
DateTime Object
(
    [date] => 0000-02-28 00:00:00.000000
    [timezone_type] => 1
    [timezone] => +00:00
)

-62162121600
-62162121600
DateTime Object
(
    [date] => 0000-02-29 00:00:00.000000
    [timezone_type] => 1
    [timezone] => +00:00
)

-62162035200
-62162035200
DateTime Object
(
    [date] => 0000-03-01 00:00:00.000000
    [timezone_type] => 1
    [timezone] => +00:00
)

-62162035200
-62162035200
DateTime Object
(
    [date] => 0000-03-01 00:00:00.000000
    [timezone_type] => 1
    [timezone] => +00:00
)

-62161948800
-62161948800
DateTime Object
(
    [date] => 0000-03-02 00:00:00.000000
    [timezone_type] => 1
    [timezone] => +00:00
)

Actual result:
--------------
-62164886400
-62164886400
DateTime Object
(
    [date] => 0000-01-28 00:00:00.000000
    [timezone_type] => 1
    [timezone] => +00:00
)

-62164800000
-62164800000
DateTime Object
(
    [date] => 0000-01-29 00:00:00.000000
    [timezone_type] => 1
    [timezone] => +00:00
)

-62164713600
-62164800000
timestamps are not equal
DateTime Object
(
    [date] => 0000-01-29 00:00:00.000000
    [timezone_type] => 1
    [timezone] => +00:00
)

-62164627200
-62164713600
timestamps are not equal
DateTime Object
(
    [date] => 0000-01-30 00:00:00.000000
    [timezone_type] => 1
    [timezone] => +00:00
)

-62164540800
-62164627200
timestamps are not equal
DateTime Object
(
    [date] => 0000-01-31 00:00:00.000000
    [timezone_type] => 1
    [timezone] => +00:00
)

-62162294400
-62162380800
timestamps are not equal
DateTime Object
(
    [date] => 0000-02-26 00:00:00.000000
    [timezone_type] => 1
    [timezone] => +00:00
)

-62162208000
-62162294400
timestamps are not equal
DateTime Object
(
    [date] => 0000-02-27 00:00:00.000000
    [timezone_type] => 1
    [timezone] => +00:00
)

-62162121600
-62162208000
timestamps are not equal
DateTime Object
(
    [date] => 0000-02-28 00:00:00.000000
    [timezone_type] => 1
    [timezone] => +00:00
)

-62162035200
-62162035200
DateTime Object
(
    [date] => 0000-03-01 00:00:00.000000
    [timezone_type] => 1
    [timezone] => +00:00
)

-62162035200
-62162035200
DateTime Object
(
    [date] => 0000-03-01 00:00:00.000000
    [timezone_type] => 1
    [timezone] => +00:00
)

-62161948800
-62161948800
DateTime Object
(
    [date] => 0000-03-02 00:00:00.000000
    [timezone_type] => 1
    [timezone] => +00:00
)

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2019-09-05 22:12 UTC] requinix@php.net
-Status: Open +Status: Duplicate
 [2019-09-05 22:12 UTC] requinix@php.net
Smells like bug #78496.
 
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Sun Dec 08 18:01:24 2019 UTC