3v4l.org

run code in 300+ PHP versions simultaneously
<?php $datetime1 = new \DateTimeImmutable('9999-01-02 15:00:00.000000', new \DateTimeZone('UTC')); try { $datetime2 = $datetime1->add(new \DateInterval('P1Y')); echo $datetime2->format('Y-m-d H:i:s.u') . \PHP_EOL; } catch (\Throwable $e) { echo 'exception' . \PHP_EOL; } $datetime3 = \DateTimeImmutable::createFromFormat('Y-m-d H:i:s.u', $datetime2->format('Y-m-d H:i:s.u'), new \DateTimeZone('UTC')); if ($datetime3 !== false) { echo $datetime3->format('Y-m-d H:i:s.u') . \PHP_EOL; } else { echo 'false' . \PHP_EOL; } try { $datetime4 = new \DateTimeImmutable($datetime2->format('Y-m-d H:i:s.u'), new \DateTimeZone('UTC')); echo $datetime4->format('Y-m-d H:i:s.u') . \PHP_EOL; } catch (\Throwable $e) { echo 'exception' . \PHP_EOL; } // Workaround with fake year try { $datetime5 = new \DateTimeImmutable($datetime2->format('1234-m-d H:i:s.u'), new \DateTimeZone('UTC')); $datetime5 = $datetime5->setDate((int)$datetime2->format("Y"), (int)$datetime2->format("n"), (int)$datetime2->format("j")); echo $datetime5->format('Y-m-d H:i:s.u') . \PHP_EOL; } catch (\Throwable $e) { echo 'exception' . \PHP_EOL; }
Output for 7.3.0 - 7.3.33, 7.4.0 - 7.4.30, 8.0.0 - 8.0.22, 8.1.0 - 8.1.9
10000-01-02 15:00:00.000000 false exception 10000-01-02 15:00:00.000000

preferences:
26.19 ms | 413 KiB | 12 Q