<?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");
$dtWithout = $dtWith;
$invert = true;
for ($i = 0; $i < 3; $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 < 3; $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.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.25, 8.4.1 - 8.4.12
- Round 0:With switch: 2021-03-28T03:00:00+02:00 :: Without switch: 2021-03-28T03:00:00+02:00
Round 1:With switch: 2021-03-28T01:00:00+01:00 :: Without switch: 2021-03-28T03:00:00+02:00
Round 2:With switch: 2021-03-28T00:00:00+01:00 :: Without switch: 2021-03-28T03:00:00+02: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
preferences:
110.89 ms | 408 KiB | 5 Q