Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Date from negative float

I have a timestamp in a variable

$data = (float) -2208988800;

Is it possible to create correct date from this data? date("d.M.Y", $data) returns "07.02.2036"

like image 794
MrBinWin Avatar asked May 18 '26 19:05

MrBinWin


1 Answers

You get result 07.02.2036 because you are on x86 (32-bit machine), where integer range is from -2147483648 to 2147483647 (see echo PHP_INT_MAX;). PHP internally cast's 2nd parameter of date() function to integer, so on 32-bit machine, string or float -2208988800 will become integer 2085978496, which is date 2036-02-07, demo.

echo date('Y-m-d', -2208988800);
# 2036-02-07 (x86)
# 1900-01-01 (x64)

run code on x86 machine

run code on x64 machine

If you wish to use negative timestamps on both machines, x86 and x64, use DateTime extension:

$dt = new DateTime('@-2208988800');
echo $dt->format('Y-m-d');

demo

Note that, for dates before the unix epoch, method getTimestamp() will return false, where method format('U') will return a correct timestamp number.

var_dump( $dt->format('U') );    # -2208988800
var_dump( $dt->getTimestamp() ); # false

demo

like image 67
Glavić Avatar answered May 21 '26 08:05

Glavić



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!