setTimezone(new \DateTimeZone('+00:00')); $oneSecond = new \DateInterval('PT1S'); // handle leap seconds if ($matches[4] === '60' && $utcDateTime->sub($oneSecond)->format('H:i:s') === '23:59:59') { $dateTime = $dateTime->sub($oneSecond); $matches[1] = str_replace(':60', ':59', $matches[1]); } // Ensure we still have the same year, month, day, hour, minutes and seconds to ensure no rollover took place. if ($dateTime->format($inputHasTSeparator ? 'Y-m-d\TH:i:s' : 'Y-m-d H:i:s') !== $matches[1]) { return null; } $mutable = \DateTime::createFromFormat('U.u', $dateTime->format('U.u')); if ($mutable === false) { throw new \RuntimeException('Unable to create DateTime from DateTimeImmutable'); } $mutable->setTimezone($dateTime->getTimezone()); return $mutable; } }