diff --git a/lib/DateTimeImmutable.php b/lib/DateTimeImmutable.php index 114ec3a3..1d103b75 100644 --- a/lib/DateTimeImmutable.php +++ b/lib/DateTimeImmutable.php @@ -35,6 +35,12 @@ public function __construct($time = 'now', $timezone = null) //switch between regular datetime and safe version public static function createFromRegular(\DateTimeImmutable $datetime): self { + // fix php 8 + if ($datetime instanceof DateTimeImmutable && $datetime->innerDateTime === null) { + $datetime->innerDateTime = new \DateTimeImmutable($datetime->format('Y-m-d H:i:s.u'), $datetime->getTimezone()); + return $datetime; + } + $safeDatetime = new self($datetime->format('Y-m-d H:i:s.u'), $datetime->getTimezone()); //we need to also update the wrapper to not break the operators '<' and '>' $safeDatetime->innerDateTime = $datetime; //to make sure we don't lose information because of the format(). return $safeDatetime; @@ -72,7 +78,7 @@ public static function createFromFormat($format, $time, $timezone = null): self public function format($format): string { /** @var string|false $result */ - $result = $this->innerDateTime->format($format); + $result = $this->innerDateTime !== null ? $this->innerDateTime->format($format) : parent::format($format); // needed for php 8 createFromRegular if ($result === false) { throw DatetimeException::createFromPhpError(); } @@ -252,7 +258,7 @@ public static function __set_state($array): self public function getTimezone(): DateTimeZone { - return $this->innerDateTime->getTimezone(); + return $this->innerDateTime !== null ? $this->innerDateTime->getTimezone() : parent::getTimezone(); // needed for php 8 createFromRegular } public function getTimestamp(): int