Skip to content
Closed
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
3 changes: 3 additions & 0 deletions EXAMPLES.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ overwrites the time-of-day of a parsed date. It composes independently of
`#[Format]`: present means "set this time", absent means "leave it untouched",
and it works on plain ISO dates too.

A date that parses only with a warning (`2026-02-30`, which PHP would roll over to
`2026-03-02`) is rejected as invalid instead of being silently normalized.

Pass a `Time` enum case for the common presets, or any valid `H:i:s` string.
Invalid time strings are rejected at metadata warm-up, not at request time.

Expand Down
6 changes: 4 additions & 2 deletions src/Coercer/DateTimeCoercer.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,10 @@ static function (mixed $val) use ($format, $time): mixed {
}
}

if (false === $result) {
return $val;
$errors = \DateTimeImmutable::getLastErrors();

if (false === $result || (false !== $errors && 0 < $errors['warning_count'])) {
return $val; // e.g. 30 February: PHP rolls it over to 2 March with only a warning
}

if (null !== $time) {
Expand Down
25 changes: 25 additions & 0 deletions tests/Unit/Coercer/DateTimeCoercerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,31 @@ public function testInvalidFormatReturnsOriginal(): void
static::assertSame('not-a-date', $coerced);
}

public function testOverflowingDateWithFormatReturnsOriginal(): void
{
// createFromFormat() rolls 30 February over to 2 March and only reports a warning; that is not a valid input.
$result = $this->coercer->coerce($this->property([new Format('!Y-m-d')]));
$coerced = ($result->coerce)('2026-02-30');

static::assertSame('2026-02-30', $coerced);
}

public function testOverflowingDateWithoutFormatReturnsOriginal(): void
{
$result = $this->coercer->coerce($this->property());
$coerced = ($result->coerce)('2026-02-30 10:00:00');

static::assertSame('2026-02-30 10:00:00', $coerced);
}

public function testTrailingGarbageWithFormatReturnsOriginal(): void
{
$result = $this->coercer->coerce($this->property([new Format('Y-m-d')]));
$coerced = ($result->coerce)('2026-02-10junk');

static::assertSame('2026-02-10junk', $coerced);
}

public function testInvalidStringWithoutFormatReturnsOriginal(): void
{
$result = $this->coercer->coerce($this->property());
Expand Down
Loading