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
- Use an ISO 8601 duration ('P1D', 'PT2H30M', 'P1W'), a DateInterval, an integer number of seconds, or a Carbon\Unit enum
- Map human vocabulary yourself: ['daily' => 'P1D', 'weekly' => 'P1W', 'monthly' => 'P1M']
- 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
- Keep a whitelist mapping config vocabulary ('daily','weekly') to ISO durations at the boundary
- Remember accepted inputs: DateInterval, ISO duration strings (P.../PT...), integer seconds, Unit enum
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
- Invalid ISO 8601 specification: {$iso}.
- Empty interval is not accepted.
- You cannot set {$unit} to a float value as {$name} would be
- Argument 1 passed to {$class}::{$method}() must be an instan
- $anchorDay parameter must not be set for $mode OverflowMode:
AI-assisted analysis of briannesbitt/Carbon@b13f05955d (2026-08-17).
Data as JSON: /api/errors/13075db09efc9673.
Report an issue: GitHub.