3v4l.org

run code in 300+ PHP versions simultaneously
<?php function incrementWithSwitch($date, $invert) { $timezone = $date->getTimezone(); $date = $date->setTimezone(new DateTimeZone('UTC')); $date = $date->modify(($invert ? '-' : '+') . '1 hour'); $date = $date->setTimezone($timezone); $date = $date->setTime((int)$date->format('H'), 0); return $date; } function incrementWithoutSwitch($date, $invert) { $date = $date->modify(($invert ? '-' : '+') . '1 hour'); return $date; } $tz = new \DateTimeZone("Europe/London"); $dtWith = \DateTimeImmutable::createFromFormat("!Y-m-d H:i:s", "2021-03-28 04:00:00", $tz); $dtWithout = $dtWith; $invert = true; for ($i = 0; $i < 4; $i++) { print "\nRound {$i}:"; $dtWith = incrementWithSwitch($dtWith, $invert); $dtWithout = incrementWithoutSwitch($dtWithout, $invert); print "With switch: ". $dtWith->format(\DateTime::RFC3339); print " :: Without switch: ". $dtWithout->format(\DateTime::RFC3339); } print "\n---\n"; $dtWith = \DateTimeImmutable::createFromFormat("!Y-m-d H:i:s", "2021-03-28 00:00:00"); $dtWithout = $dtWith; $invert = false; for ($i = 0; $i < 4; $i++) { print "\nRound {$i}:"; $dtWith = incrementWithSwitch($dtWith, $invert); $dtWithout = incrementWithoutSwitch($dtWithout, $invert); print "With switch: ". $dtWith->format(\DateTime::RFC3339); print " :: Without switch: ". $dtWithout->format(\DateTime::RFC3339); }
Output for 7.3.0 - 7.3.31, 7.3.33, 7.4.0 - 7.4.25, 7.4.27 - 7.4.33, 8.0.0 - 8.0.12, 8.0.14 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.4, 8.3.6
Round 0:With switch: 2021-03-28T03:00:00+01:00 :: Without switch: 2021-03-28T03:00:00+01:00 Round 1:With switch: 2021-03-28T02:00:00+01:00 :: Without switch: 2021-03-28T02:00:00+01:00 Round 2:With switch: 2021-03-28T00:00:00+00:00 :: Without switch: 2021-03-28T02:00:00+01:00 Round 3:With switch: 2021-03-27T23:00:00+00:00 :: Without switch: 2021-03-28T02:00:00+01:00 --- Round 0:With switch: 2021-03-28T01:00:00+01:00 :: Without switch: 2021-03-28T01:00:00+01:00 Round 1:With switch: 2021-03-28T03:00:00+02:00 :: Without switch: 2021-03-28T03:00:00+02:00 Round 2:With switch: 2021-03-28T04:00:00+02:00 :: Without switch: 2021-03-28T04:00:00+02:00 Round 3:With switch: 2021-03-28T05:00:00+02:00 :: Without switch: 2021-03-28T05:00:00+02:00
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 Round 0:With switch: 2021-03-28T03:00:00+01:00 :: Without switch: 2021-03-28T03:00:00+01:00 Round 1:With switch: 2021-03-28T02:00:00+01:00 :: Without switch: 2021-03-28T02:00:00+01:00 Round 2:With switch: 2021-03-28T00:00:00+00:00 :: Without switch: 2021-03-28T02:00:00+01:00 Round 3:With switch: 2021-03-27T23:00:00+00:00 :: Without switch: 2021-03-28T02:00:00+01:00 --- Round 0:With switch: 2021-03-28T01:00:00+01:00 :: Without switch: 2021-03-28T01:00:00+01:00 Round 1:With switch: 2021-03-28T03:00:00+02:00 :: Without switch: 2021-03-28T03:00:00+02:00 Round 2:With switch: 2021-03-28T04:00:00+02:00 :: Without switch: 2021-03-28T04:00:00+02:00 Round 3:With switch: 2021-03-28T05:00:00+02:00 :: Without switch: 2021-03-28T05:00:00+02:00
Output for 7.3.32, 7.4.26, 8.0.13
Round 0:With switch: 2021-03-28T03:00:00+01:00 :: Without switch: 2021-03-28T03:00:00+01:00 Round 1:With switch: 2021-03-28T02:00:00+01:00 :: Without switch: 2021-03-28T02:00:00+01:00 Round 2:With switch: 2021-03-28T00:00:00+00:00 :: Without switch: 2021-03-28T02:00:00+01:00 Round 3:With switch: 2021-03-27T23:00:00+00:00 :: Without switch: 2021-03-28T02:00:00+01:00 --- Round 0:With switch: 2021-03-28T01:00:00+00:00 :: Without switch: 2021-03-28T01:00:00+00:00 Round 1:With switch: 2021-03-28T02:00:00+00:00 :: Without switch: 2021-03-28T02:00:00+00:00 Round 2:With switch: 2021-03-28T03:00:00+00:00 :: Without switch: 2021-03-28T03:00:00+00:00 Round 3:With switch: 2021-03-28T04:00:00+00:00 :: Without switch: 2021-03-28T04:00:00+00:00

preferences:
160.79 ms | 403 KiB | 154 Q