{"record":{"id":"13075db09efc9673","repo":"briannesbitt/Carbon","slug":"invalid-interval","errorCode":null,"errorMessage":"Invalid interval.","messagePattern":"Invalid interval\\.","errorType":"exception","errorClass":"InvalidIntervalException","httpStatus":null,"severity":"error","filePath":"src/Carbon/CarbonPeriod.php","lineNumber":990,"sourceCode":"     * @param DateInterval|Unit|string|int $interval\n     * @param Unit|string                  $unit     the unit of $interval if it's a number\n     *\n     * @throws InvalidIntervalException\n     *\n     * @return static\n     */\n    public function setDateInterval(mixed $interval, Unit|string|null $unit = null): static\n    {\n        if ($interval instanceof Unit) {\n            $interval = $interval->interval();\n        }\n\n        if ($unit instanceof Unit) {\n            $unit = $unit->name;\n        }\n\n        if (!$interval = CarbonInterval::make($interval, $unit)) {\n            throw new InvalidIntervalException('Invalid interval.');\n        }\n\n        if ($interval->spec() === 'PT0S' && !$interval->f && !$interval->getStep()) {\n            throw new InvalidIntervalException('Empty interval is not accepted.');\n        }\n\n        $self = $this->copyIfImmutable();\n        $self->dateInterval = $interval;\n\n        $self->isDefaultInterval = false;\n\n        $self->handleChangedParameters();\n\n        return $self;\n    }\n\n    /**\n     * Reset the date interval to the default value.","sourceCodeStart":972,"sourceCodeEnd":1008,"githubUrl":"https://github.com/briannesbitt/Carbon/blob/b13f05955dcfd7da71d60af745fd148cbdb73505/src/Carbon/CarbonPeriod.php#L972-L1008","documentation":"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).","triggerScenarios":"$period->setDateInterval('weekly'); ->setDateInterval(null); ->setDateInterval('') — any value CarbonInterval::make() cannot turn into an interval; durations missing the P/PT prefix like '2 days'.","commonSituations":"Config keys like ('frequency' => 'weekly') forwarded straight into setDateInterval; empty request parameters; human words mistaken for ISO durations.","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"],"exampleFix":"// before\n$period->setDateInterval($config['frequency']); // 'weekly'\n\n// after\n$map = ['daily' => 'P1D', 'weekly' => 'P1W', 'monthly' => 'P1M'];\n$period->setDateInterval($map[$config['frequency']] ?? 'P1D');","handlingStrategy":"validation","validationCode":"if (CarbonInterval::make($intervalInput) === null) {\n    throw new InvalidArgumentException('Cannot interpret interval: '.var_export($intervalInput, true));\n}\n$period->setDateInterval($intervalInput);","typeGuard":null,"tryCatchPattern":"try {\n    $period->setDateInterval($config['interval']);\n} catch (\\Carbon\\Exceptions\\InvalidIntervalException $e) {\n    $period->setDateInterval('P1D'); // safe default + log the bad config\n}","preventionTips":["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"],"tags":["carbon","carbon-period","carbon-interval","interval","string-parsing"],"backgroundTag":"interval-parse-error","analyzedSha":"b13f05955dcfd7da71d60af745fd148cbdb73505","analyzedAt":"2026-08-17T04:40:56.953Z","schemaVersion":2},"datasetVersion":"2026-08-17T09:17:11.063Z"}