Skip to content
Merged
Show file tree
Hide file tree
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
13 changes: 13 additions & 0 deletions library/Helpers/CanValidateDateTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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,
Expand All @@ -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
*/
Expand Down
3 changes: 3 additions & 0 deletions tests/unit/Helpers/CanValidateDateTimeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

namespace Respect\Validation\Helpers;

use DateTime;
use Respect\Validation\Test\TestCase;

/**
Expand Down Expand Up @@ -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'],
];
}

Expand Down