symfony/finder · error · InvalidArgumentException

" " is not a valid date.

Error message

"%s" is not a valid date.

What it means

DateComparator successfully matched an operator prefix but new DateTimeImmutable($matches[2]) could not parse the remaining text, so it throws InvalidArgumentException declaring the string is not a valid date. The library relies on PHP's date parsing, so any unparseable date expression triggers this.

Solutions

  1. Use a date expression PHP DateTimeImmutable can parse: 'yesterday', '-7 days', '2024-01-01', 'since Monday'
  2. Test the string first with new \DateTimeImmutable($date) in a try/catch before building the comparator
  3. Prefer absolute ISO dates (Y-m-d or ISO 8601) in configs to avoid relative-expression ambiguity

Example fix

// before
$finder->date('since yestreday'); // unparseable
// after
$finder->date('since yesterday');
Defensive patterns

Strategy: validation

Validate before calling

try { new \DateTimeImmutable($datePart); } catch (\Exception $e) { /* reject: not a valid date */ }

Type guard

function isParsableDate(string $s): bool { try { new \DateTimeImmutable($s); return true; } catch (\Exception) { return false; } }

Try / catch

try { $finder->date($test); } catch (\InvalidArgumentException $e) { /* handle unparseable date */ }

Prevention

When it happens

Trigger: new DateComparator('since someday') or Finder::date('> not-a-date') where the date portion (e.g. 'someday', 'next someday', typo like '2024-13-45') fails DateTimeImmutable parsing.

Common situations: Typos in relative date expressions ('yestreday'), locale-specific date strings not supported by PHP, passing human text like 'a week ago and a half', copy-pasted dates with stray characters.

Related errors


AI-assisted analysis of symfony/finder@4d6c057bfd (2026-09-13). Data as JSON: /api/errors/fa2d1363a568545d. Report an issue: GitHub.

Appendix: source

Thrown at Comparator/DateComparator.php:36

 */
class DateComparator extends Comparator
{
    /**
     * @param string $test A comparison string
     *
     * @throws \InvalidArgumentException If the test is not understood
     */
    public function __construct(string $test)
    {
        if (!preg_match('#^\s*(==|!=|[<>]=?|after|since|before|until)?\s*(.+?)\s*$#i', $test, $matches)) {
            throw new \InvalidArgumentException(\sprintf('Don\'t understand "%s" as a date test.', $test));
        }

        try {
            $date = new \DateTimeImmutable($matches[2]);
            $target = $date->format('U');
        } catch (\Exception) {
            throw new \InvalidArgumentException(\sprintf('"%s" is not a valid date.', $matches[2]));
        }

        $operator = $matches[1] ?: '==';
        if ('since' === $operator || 'after' === $operator) {
            $operator = '>';
        }

        if ('until' === $operator || 'before' === $operator) {
            $operator = '<';
        }

        parent::__construct($target, $operator);
    }
}

View on GitHub (pinned to 4d6c057bfd)