Skip to content

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

Closed
@mvorisek

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

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions