Skip to content

DateTime[Immutable]::createFromTimestamp(float) misses support for 64bit integers or 32bit environments #20967

@marc-mabe

Description

@marc-mabe

Description

The following code:

<?php

$dt = DateTime::createFromFormat('U', '2147483647');
var_dump($dt);

$dt = $dt->modify('+1 second');
var_dump($dt);

$ts = $dt->format('U');
$tsf = (float)$ts;
var_dump($ts, $tsf);

$dt2 = DateTime::createFromTimestamp($tsf);

Resulted in this output:

object(DateTime)#1 (3) {
  ["date"]=>
  string(26) "2038-01-19 03:14:07.000000"
  ["timezone_type"]=>
  int(1)
  ["timezone"]=>
  string(6) "+00:00"
}
object(DateTime)#1 (3) {
  ["date"]=>
  string(26) "2038-01-19 03:14:08.000000"
  ["timezone_type"]=>
  int(1)
  ["timezone"]=>
  string(6) "+00:00"
}
string(10) "2147483648"
float(2147483648)

Fatal error: Uncaught DateRangeError: DateTime::createFromTimestamp(): Argument #1 ($timestamp) must be a finite number between -2147483648 and 2147483647.999999, 2.14748e+9 given 

But I expected this output instead:

object(DateTime)#1 (3) {
  ["date"]=>
  string(26) "2038-01-19 03:14:07.000000"
  ["timezone_type"]=>
  int(1)
  ["timezone"]=>
  string(6) "+00:00"
}
object(DateTime)#1 (3) {
  ["date"]=>
  string(26) "2038-01-19 03:14:08.000000"
  ["timezone_type"]=>
  int(1)
  ["timezone"]=>
  string(6) "+00:00"
}
string(10) "2147483648"
float(2147483648)
object(DateTime)#2 (3) {
  ["date"]=>
  string(26) "2038-01-19 03:14:08.000000"
  ["timezone_type"]=>
  int(1)
  ["timezone"]=>
  string(6) "+00:00"
}

PHP Version

>= PHP 8.4

Operating System

32bit environment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions