{"record":{"id":"ffcbab1d8c2a7b4e","repo":"briannesbitt/Carbon","slug":"interval-objects-cannot-be-multiplied-by-a-non-int","errorCode":null,"errorMessage":"Interval objects cannot be multiplied by a non-integer value.","messagePattern":"Interval objects cannot be multiplied by a non-integer value\\.","errorType":"exception","errorClass":"InvalidArgumentException","httpStatus":null,"severity":"error","filePath":"src/Carbon/Traits/Units.php","lineNumber":609,"sourceCode":"    }\n\n    /**\n     * Set current day of the instance to the passed value if it exits in the\n     * current month, else set current day to the last day of the month.\n     */\n    public function setAnchorDay(int $anchorDay): static\n    {\n        if ($anchorDay < 1) {\n            throw new InvalidArgumentException('$anchorDay must be greater than 0');\n        }\n\n        return $this->day(min($anchorDay, $this->daysInMonth));\n    }\n\n    private static function disallowDecimalPart(mixed $value): void\n    {\n        if (((float) $value) !== ((float) (int) $value)) {\n            throw new InvalidArgumentException(\n                'Interval objects cannot be multiplied by a non-integer value.',\n            );\n        }\n    }\n\n    private function getOverflowMode(\n        OverflowMode|bool|null $overflow = null,\n        ?int $anchorDay = null,\n    ): ?OverflowMode {\n        if ($anchorDay !== null) {\n            $overflow ??= OverflowMode::AnchorDay;\n\n            if ($overflow !== OverflowMode::AnchorDay) {\n                throw new InvalidArgumentException(\n                    '$anchorDay can be set only $overflow = OverflowMode::AnchorDay',\n                );\n            }\n        }","sourceCodeStart":591,"sourceCodeEnd":627,"githubUrl":"https://github.com/briannesbitt/Carbon/blob/b13f05955dcfd7da71d60af745fd148cbdb73505/src/Carbon/Traits/Units.php#L591-L627","documentation":"When the first argument of add()/sub() is an interval-like value (CarbonInterval, DateInterval converted to closure, CarbonConverterInterface or a Closure), Carbon applies it N times in a loop where the count must be a whole number. disallowDecimalPart() compares the float value against its int cast and throws InvalidArgumentException for any fractional multiplier such as 1.5 or 0.5 - you cannot apply an interval one and a half times.","triggerScenarios":"$date->add(CarbonInterval::days(2), 1.5); $date->add($someIntervalConverter, 0.5); a computed multiplier like $total / $chunkSize that lands on 2.25; passing '1.5' as a numeric string multiplier; passing -1.5 to invert and scale an interval in sub().","commonSituations":"Generic 'repeat interval X times' helpers receiving user quotas that are not multiples of the chunk; batch/split logic computing counts by division; migrating code that multiplied CarbonInterval::times(...) (which supports floats on some units) into the add(..., $times) signature which does not.","solutions":["Convert the fraction into smaller units before adding: instead of add(CarbonInterval::days(2), 1.5) use ->addUnit('hour', 72) or CarbonInterval::hours(72)","Round/cast the multiplier deliberately: (int) floor($times) or ceil() per business rule, and handle the remainder explicitly","If you need fractional interval scaling, use CarbonInterval's own float-capable multiplication (e.g. CarbonInterval::fromString('2 days')->times(1.5)) and add the resulting interval once","Validate fmod($times, 1) === 0.0 at the boundary when the count comes from division"],"exampleFix":"// before\n$date->add(CarbonInterval::days(2), 1.5); // throws\n\n// after\n$date->add(CarbonInterval::hours(72)); // 1.5 x 2 days expressed exactly","handlingStrategy":"validation","validationCode":"$times = (float) $count;\nif (fmod($times, 1.0) !== 0.0) {\n    throw new InvalidArgumentException(\"Interval repeat count must be an integer, got $count\");\n}\n$date->add($interval, (int) $times);","typeGuard":"function isIntegerMultiplier(int|float|string $value): bool\n{\n    return is_int($value) || (is_float($value) && fmod($value, 1.0) === 0.0)\n        || (is_string($value) && preg_match('/^-?\\d+$/', $value) === 1);\n}","tryCatchPattern":"try {\n    $date->add($interval, $times);\n} catch (\\InvalidArgumentException $e) {\n    // fractional count: split into whole repeats + remainder in smaller units\n    $date->add($interval, (int) floor($times));\n    $date->addUnit('hour', $interval->totalHours * fmod($times, 1.0));\n}","preventionTips":["Express fractional durations in smaller units (hours/minutes) instead of fractional multipliers","When the count comes from division, decide floor vs ceil explicitly and handle the remainder","For true float scaling, use CarbonInterval's float-capable times() and add the result once"],"tags":["php","carbon","type-validation","interval-arithmetic"],"backgroundTag":"non-integer-multiplier","analyzedSha":"b13f05955dcfd7da71d60af745fd148cbdb73505","analyzedAt":"2026-08-17T04:40:56.953Z","schemaVersion":2},"datasetVersion":"2026-08-17T09:17:11.063Z"}