Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions lib/DateTimeImmutable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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
Expand Down