3v4l.org

run code in 300+ PHP versions simultaneously
<?php function createDtFromTimestamp(float $timestamp): \DateTime { $timestamp = round($timestamp * 1e6) * 1e-6; $sec = (int) sprintf('%0.0f', floor($timestamp)); $usec = (int) sprintf('%0.0f', min(max(($timestamp - $sec) * 1e6, 0), 999999)); $datetime = new \DateTime(date('Y-m-d H:i:s.', $sec) . sprintf('%06d', $usec)); return $datetime; } function dumpDt(\DateTime $dt) { echo $dt->format('Y-m-d H:i:s.u') . ' ' . $dt->getTimestamp() . ' ' . $dt->format('u') . "\n"; } foreach ([0.4, 0, -0.4, -1, -1.4] as $ts) { $dateExpected = createDtFromTimestamp($ts); dumpDt($dateExpected); $date = new DateTime('@' . $ts); dumpDt($date); echo "\n"; }
Output for 8.1.7 - 8.1.28, 8.2.0 - 8.2.19, 8.3.0 - 8.3.4, 8.3.6 - 8.3.7
1970-01-01 01:00:00.400000 0 400000 1970-01-01 00:00:00.400000 0 400000 1970-01-01 01:00:00.000000 0 000000 1970-01-01 00:00:00.000000 0 000000 1970-01-01 00:59:59.600000 -1 600000 1969-12-31 23:59:59.600000 -1 600000 1970-01-01 00:59:59.000000 -1 000000 1969-12-31 23:59:59.000000 -1 000000 1970-01-01 00:59:58.600000 -2 600000 1969-12-31 23:59:58.600000 -2 600000
Output for 8.3.5
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0 1970-01-01 01:00:00.400000 0 400000 1970-01-01 00:00:00.400000 0 400000 1970-01-01 01:00:00.000000 0 000000 1970-01-01 00:00:00.000000 0 000000 1970-01-01 00:59:59.600000 -1 600000 1969-12-31 23:59:59.600000 -1 600000 1970-01-01 00:59:59.000000 -1 000000 1969-12-31 23:59:59.000000 -1 000000 1970-01-01 00:59:58.600000 -2 600000 1969-12-31 23:59:58.600000 -2 600000
Output for 8.0.0 - 8.0.30, 8.1.0 - 8.1.6
1970-01-01 01:00:00.400000 0 400000 1970-01-01 00:00:00.400000 0 400000 1970-01-01 01:00:00.000000 0 000000 1970-01-01 00:00:00.000000 0 000000 1970-01-01 00:59:59.600000 -1 600000 1970-01-01 00:00:00.400000 0 400000 1970-01-01 00:59:59.000000 -1 000000 1969-12-31 23:59:59.000000 -1 000000 1970-01-01 00:59:58.600000 -2 600000 1969-12-31 23:59:59.400000 -1 400000
Output for 7.3.13 - 7.3.33, 7.4.0 - 7.4.33
1970-01-01 01:00:00.400000 0 400000 Fatal error: Uncaught Exception: DateTime::__construct(): Failed to parse time string (@0.4) at position 3 (4): Unexpected character in /in/CjJLf:20 Stack trace: #0 /in/CjJLf(20): DateTime->__construct('@0.4') #1 {main} thrown in /in/CjJLf on line 20
Process exited with code 255.

preferences:
143 ms | 402 KiB | 145 Q