diff --git a/library/Helpers/CanValidateDateTime.php b/library/Helpers/CanValidateDateTime.php index 15a08eff0..eab088c41 100644 --- a/library/Helpers/CanValidateDateTime.php +++ b/library/Helpers/CanValidateDateTime.php @@ -16,6 +16,9 @@ use function date_default_timezone_get; use function date_parse_from_format; use function preg_match; +use function strlen; +use function strrpos; +use function substr; /** * Helper to handle date/time. @@ -43,6 +46,10 @@ private function isDateTime(string $format, string $value): bool } if ($this->isDateFormat($format)) { + if ($this->needsZuluTimezoneReplacement($format, $value)) { + $value = substr($value, 0, -1) . '+00:00'; + } + $formattedDate = DateTime::createFromFormat( '!' . $format, $value, @@ -59,6 +66,12 @@ private function isDateTime(string $format, string $value): bool return true; } + private function needsZuluTimezoneReplacement(string $format, string $value): bool + { + return ($format === DateTime::RFC3339_EXTENDED || $format === DateTime::RFC3339) + && strrpos($value, 'Z') === strlen($value) - 1; + } + /** * @param mixed[] $info */ diff --git a/tests/unit/Helpers/CanValidateDateTimeTest.php b/tests/unit/Helpers/CanValidateDateTimeTest.php index 8ec661874..c292b29e3 100644 --- a/tests/unit/Helpers/CanValidateDateTimeTest.php +++ b/tests/unit/Helpers/CanValidateDateTimeTest.php @@ -9,6 +9,7 @@ namespace Respect\Validation\Helpers; +use DateTime; use Respect\Validation\Test\TestCase; /** @@ -57,6 +58,8 @@ public static function providerForValidDateTime(): array ['Y-m-d\TH:i:sP', '2018-01-30T19:04:35+00:00'], ['r', 'Tue, 30 Jan 2018 19:06:01 +0000'], ['D, d M Y H:i:s O', 'Tue, 30 Jan 2018 19:06:01 +0000'], + [DateTime::RFC3339_EXTENDED, '2014-04-12T23:20:50.052Z'], + [DateTime::RFC3339, '2005-08-15T15:52:01Z'], ]; }