Skip to content

Datetime created from negative float timestamp is not monotonic (fraction part is added instead of substracted) #7758

Closed

Description

@mvorisek

Description

The following code:

foreach ([0.4, 0, -0.4, -1, -1.4] as $ts) {
    $date = new DateTime('@' . $ts);
    print_r($date);
}

https://3v4l.org/EWpSj

Diff of current output vs expected:

 DateTime Object
 (
     [date] => 1970-01-01 00:00:00.400000
     [timezone_type] => 1
     [timezone] => +00:00
 )
 DateTime Object
 (
     [date] => 1970-01-01 00:00:00.000000
     [timezone_type] => 1
     [timezone] => +00:00
 )
 DateTime Object
 (
-    [date] => 1970-01-01 00:00:00.400000
+    [date] => 1969-12-31 23:59:59.600000
     [timezone_type] => 1
     [timezone] => +00:00
 )
 DateTime Object
 (
     [date] => 1969-12-31 23:59:59.000000
     [timezone_type] => 1
     [timezone] => +00:00
 )
 DateTime Object
 (
-    [date] => 1969-12-31 23:59:59.400000
+    [date] => 1969-12-31 23:59:58.600000
     [timezone_type] => 1
     [timezone] => +00:00
 )

PHP Version

present since PHP 8.0, should be fixed starting this version

it seems the issue is present only when TS is parsed, getTimestamp / format('u') seems correct across all PHP versions - https://3v4l.org/CjJLf

Activity

  1. damianwadley commented on Dec 11, 2021

    @damianwadley
    Member

    For the explanation: fractional timestamps are currently not supported when parsing, which means PHP handles inputs like @-1.4 as an absolute "@-1" (1970 minus 1s) followed by a relative ".4" (plus 0.4s).

  2. mvorisek commented on Dec 13, 2021

    @mvorisek
    ContributorAuthor

    For the explanation: fractional timestamps are currently not supported when parsing, which means PHP handles inputs like @-1.4 as an absolute "@-1" (1970 minus 1s) followed by a relative ".4" (plus 0.4s).

    this is how it is parsed currently probably, however if unix timestamp is negative, the fractional part must be decremented as if the whole TS was a standard floating point number

    https://3v4l.org/rKOAb shows the fractional part cannot be even used alone (eg. parsed with @ts token and the parsing must be fixed)

  3. iluuu1994 commented on Dec 13, 2021

    @iluuu1994
    Member

    I think it's fair to say that this a feature request as the docs do state that only whole numbers are supported.

  4. self-assigned this
    on Feb 4, 2022
  5. assigned and unassigned on Apr 1, 2022
  6. hormus commented on Apr 24, 2022

    @hormus
  7. added a commit that references this issue on May 20, 2022
  8. added a commit that references this issue on Oct 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions