3v4l.org

run code in 300+ PHP versions simultaneously
<?php class DateTimeType { private const FORMAT = 'Y-m-d H:i:s'; private const TIMEZONE = 'Europe/Berlin'; public static function isValidDateTimeOld(string $value): bool { $date = DateTime::createFromFormat(self::FORMAT, $value, new DateTimeZone(self::TIMEZONE)); $errors = DateTime::getLastErrors(); return $date && $errors['warning_count'] === 0 && $errors['error_count'] === 0; } public static function isValidDateTimeNew(string $value): bool { $date = DateTime::createFromFormat(self::FORMAT, $value, new DateTimeZone(self::TIMEZONE)); $errors = DateTime::getLastErrors(); return $date && !$errors; } } $dateTimeToCheck = '2022-02-17 13:37:00'; var_dump(DateTimeType::isValidDateTimeOld($dateTimeToCheck)); var_dump(DateTimeType::isValidDateTimeNew($dateTimeToCheck));
Output for 8.3.0 - 8.3.25, 8.4.1 - 8.4.12
Warning: Trying to access array offset on false in /in/hFvIJ on line 14 bool(false) bool(true)
Output for 8.2.0 - 8.2.29
Warning: Trying to access array offset on value of type bool in /in/hFvIJ on line 14 bool(false) bool(true)
Output for 7.4.0 - 7.4.33, 8.0.1 - 8.0.30, 8.1.0 - 8.1.33
bool(true) bool(false)

preferences:
135.65 ms | 409 KiB | 5 Q