briannesbitt/Carbon · error · InvalidIntervalException

Invalid interval.

Error message

Invalid interval.

What it means

setDateInterval() converts its input via CarbonInterval::make(), which accepts a DateInterval/CarbonInterval, an ISO 8601 duration string ('P1D', 'PT3H'), a number of seconds, a Unit enum, or a string/interval in the given $unit. When the input cannot be converted (make() returns null — e.g. null, '', an unparseable word), 'Invalid interval.' is thrown (src/Carbon/CarbonPeriod.php:990).

Source

Thrown at src/Carbon/CarbonPeriod.php:990

     * @param DateInterval|Unit|string|int $interval
     * @param Unit|string                  $unit     the unit of $interval if it's a number
     *
     * @throws InvalidIntervalException
     *
     * @return static
     */
    public function setDateInterval(mixed $interval, Unit|string|null $unit = null): static
    {
        if ($interval instanceof Unit) {
            $interval = $interval->interval();
        }

        if ($unit instanceof Unit) {
            $unit = $unit->name;
        }

        if (!$interval = CarbonInterval::make($interval, $unit)) {
            throw new InvalidIntervalException('Invalid interval.');
        }

        if ($interval->spec() === 'PT0S' && !$interval->f && !$interval->getStep()) {
            throw new InvalidIntervalException('Empty interval is not accepted.');
        }

        $self = $this->copyIfImmutable();
        $self->dateInterval = $interval;

        $self->isDefaultInterval = false;

        $self->handleChangedParameters();

        return $self;
    }

    /**
     * Reset the date interval to the default value.

View on GitHub (pinned to b13f05955d)

Solutions

  1. Use an ISO 8601 duration ('P1D', 'PT2H30M', 'P1W'), a DateInterval, an integer number of seconds, or a Carbon\Unit enum
  2. Map human vocabulary yourself: ['daily' => 'P1D', 'weekly' => 'P1W', 'monthly' => 'P1M']
  3. Pre-test the value: if (CarbonInterval::make($input) === null) reject before calling

Example fix

// before
$period->setDateInterval($config['frequency']); // 'weekly'

// after
$map = ['daily' => 'P1D', 'weekly' => 'P1W', 'monthly' => 'P1M'];
$period->setDateInterval($map[$config['frequency']] ?? 'P1D');
Defensive patterns

Strategy: validation

Validate before calling

if (CarbonInterval::make($intervalInput) === null) {
    throw new InvalidArgumentException('Cannot interpret interval: '.var_export($intervalInput, true));
}
$period->setDateInterval($intervalInput);

Try / catch

try {
    $period->setDateInterval($config['interval']);
} catch (\Carbon\Exceptions\InvalidIntervalException $e) {
    $period->setDateInterval('P1D'); // safe default + log the bad config
}

Prevention

When it happens

Trigger: $period->setDateInterval('weekly'); ->setDateInterval(null); ->setDateInterval('') — any value CarbonInterval::make() cannot turn into an interval; durations missing the P/PT prefix like '2 days'.

Common situations: Config keys like ('frequency' => 'weekly') forwarded straight into setDateInterval; empty request parameters; human words mistaken for ISO durations.

Related errors


AI-assisted analysis of briannesbitt/Carbon@b13f05955d (2026-08-17). Data as JSON: /api/errors/13075db09efc9673. Report an issue: GitHub.