3v4l.org

run code in 300+ PHP versions simultaneously
<?php $test = static function (string $expected, int $precision): void { $date = DateTimeImmutable::createFromFormat('U.u', $expected); echo 'Testing "', $date->format('Y-m-d H:i:s.u'), '" with given precision (', $precision, ')', PHP_EOL; ini_set('serialize_precision', $precision); $encoded = json_encode((float) $date->format('U.u')); var_dump($expected, $encoded, $expected === $encoded); if ($expected === $encoded) { echo PHP_EOL; return; } $adjustedPrecision = strlen($date->format('Uu')); echo 'Testing with adjusted precision (', $adjustedPrecision, ')', PHP_EOL; ini_set('serialize_precision', $adjustedPrecision); $encoded = json_encode((float) $date->format('U.u')); var_dump($expected, $encoded, $expected === $encoded); echo PHP_EOL; }; // $test('1487285080.123456', 8); // $test('1487285080.123456', 16); // $test('11487285080.123456', 16); // $test('112487285080.123456', -1); // $test('9999999999.999999', -1); $test('9999999999.999994', -1); $test('9999999999.999995', -1);

preferences:
34.62 ms | 406 KiB | 5 Q