mongodb/laravel-mongodb · error · InvalidArgumentException

Invalid time format, expected HH:MM:SS, HH:MM or HH, got

Error message

Invalid time format, expected HH:MM:SS, HH:MM or HH, got "%s"

What it means

compileWhereTime() accepts only string times matching HH, HH:MM or HH:MM:SS (hours 00-29 syntactically) so it can map them to a $dateToString format for time comparison. Anything else — non-strings or malformed strings — throws InvalidArgumentException.

Solutions

  1. Format the value first: $dt->format('H:i:s') before passing to whereTime().
  2. Zero-pad hours: '09:30' not '9:30'.
  3. Pass exactly one of the supported shapes: 'HH', 'HH:MM', 'HH:MM:SS'.
  4. For non-string values, convert with sprintf('%02d:%02d:%02d', $h, $m, $s) or use ->whereRaw / date aggregation instead.

Example fix

// before
$query->whereTime('created_at', '>=', '9:30 AM');
// after
$query->whereTime('created_at', '>=', '09:30');
Defensive patterns

Strategy: validation

Validate before calling

function normalizeTime(mixed $t): string {
    if ($t instanceof \DateTimeInterface) return $t->format('H:i:s');
    if (is_string($t) && preg_match('/^([01]?\d|2\d):([0-5]\d)(?::([0-5]\d))?$/', $t, $m)) {
        return sprintf('%02d:%s:%s', $m[1], $m[2], $m[3] ?? '00');
    }
    throw new InvalidArgumentException('Time must be HH, HH:MM or HH:MM:SS');
}

Prevention

When it happens

Trigger: ->whereTime('created_at', '>=', '9:30') (single-digit hour); ->whereTime('t', '=', '09:30:00.123') (fractional seconds); ->whereTime('t', '=', 930) (int); Carbon/DateTime objects passed instead of strings.

Common situations: Passing time values from user input or DB numeric columns; passing DateTimeInterface instances expecting automatic conversion; locale-formatted times like '9:30 AM'.

Related errors


AI-assisted analysis of mongodb/laravel-mongodb@0634653039 (2026-09-15). Data as JSON: /api/errors/510fc1a96a06351f. Report an issue: GitHub.

Appendix: source

Thrown at src/Query/Builder.php:1682

    protected function compileWhereYear(array $where): array
    {
        return [
            '$expr' => [
                '$' . $where['operator'] => [
                    [
                        '$year' => '$' . $where['column'],
                    ],
                    (int) $where['value'],
                ],
            ],
        ];
    }

    protected function compileWhereTime(array $where): array
    {
        if (! is_string($where['value']) || ! preg_match('/^[0-2][0-9](:[0-6][0-9](:[0-6][0-9])?)?$/', $where['value'], $matches)) {
            throw new InvalidArgumentException(sprintf('Invalid time format, expected HH:MM:SS, HH:MM or HH, got "%s"', is_string($where['value']) ? $where['value'] : get_debug_type($where['value'])));
        }

        $format = match (count($matches)) {
            1 => '%H',
            2 => '%H:%M',
            3 => '%H:%M:%S',
        };

        return [
            '$expr' => [
                '$' . $where['operator'] => [
                    [
                        '$dateToString' => ['date' => '$' . $where['column'], 'format' => $format],
                    ],
                    $where['value'],
                ],
            ],
        ];

View on GitHub (pinned to 0634653039)