3v4l.org

run code in 300+ PHP versions simultaneously
<?php readonly class MySqlTime { public function __construct( public int $hours, public int $minutes, public int $seconds, ) { } public static function fromMySqlTimeAsDurationInDay(string $time): self { [$hours, $minutes, $seconds] = array_map('intval', explode(':', $time)); // Ensure only positive values on a single day. // MySql should already be throwing errors about minutes or seconds being out of range, so we don't // need to check those. // The seconds could include decimals, however for this purpose we are ignoring them. if ($hours > 23 || $hours < 0) { throw new InvalidArgumentException('Hours must be between 0 and 23'); } return new self($hours, $minutes, $seconds); } public function toDateTime(DateTimeInterface $dateTime = null) : DateTimeInterface { $dateTime ??= new DateTimeImmutable(); return $dateTime->setTime($this->hours, $this->minutes, $this->seconds); } } var_dump(MySqlTime::fromMySqlTimeAsDurationInDay('12:34:56')->toDateTime());
Output for 8.4.1 - 8.4.12
Deprecated: MySqlTime::toDateTime(): Implicitly marking parameter $dateTime as nullable is deprecated, the explicit nullable type must be used instead in /in/B6mr4 on line 27 object(DateTimeImmutable)#3 (3) { ["date"]=> string(26) "2023-07-31 12:34:56.000000" ["timezone_type"]=> int(3) ["timezone"]=> string(16) "Europe/Amsterdam" }
Output for 8.2.7 - 8.2.29, 8.3.5 - 8.3.25
object(DateTimeImmutable)#3 (3) { ["date"]=> string(26) "2023-07-31 12:34:56.000000" ["timezone_type"]=> int(3) ["timezone"]=> string(16) "Europe/Amsterdam" }
Output for 8.1.30 - 8.1.33
Parse error: syntax error, unexpected token "readonly", expecting end of file in /in/B6mr4 on line 3
Process exited with code 255.

preferences:
61.25 ms | 408 KiB | 5 Q