briannesbitt/Carbon · error · InvalidArgumentException

$unit cannot be changed to float value $value, integer expec

Error message

$unit cannot be changed to float value $value, integer expected

What it means

Error "$unit cannot be changed to float value $value, integer expected" thrown in briannesbitt/Carbon.

Source

Thrown at src/Carbon/Traits/Date.php:2537

     * @param string                 $unit      year, month, day, hour, minute, second or microsecond
     * @param Month|int|float|null   $value     new value for given unit
     * @param OverflowMode|bool|null $overflow  either overflow (when the day does not exist in current month)
     *                                          is allowed or not, or should use an anchor day
     * @param ?int                   $anchorDay the day to which to go after setting year/quarter/month,
     *                                          if given but it does not exist in current month, it will
     *                                          go to the last day of the month
     */
    public function setUnit(
        string $unit,
        Month|int|float|null $value = null,
        OverflowMode|bool|null $overflow = null,
        ?int $anchorDay = null,
    ): static {
        if (\is_float($value)) {
            $int = (int) $value;

            if ((float) $int !== $value) {
                throw new InvalidArgumentException(
                    "$unit cannot be changed to float value $value, integer expected",
                );
            }

            $value = $int;
        }

        $overflow = $this->getOverflowMode($overflow, $anchorDay);
        $unit = static::singularUnit($unit);
        $value = self::monthToInt($value, $unit);
        $dateUnits = ['year', 'month', 'day'];

        if (\in_array($unit, $dateUnits)) {
            if ($unit !== 'day') {
                $overflow ??= $this->shouldUnitOverflow($unit)
                    ? OverflowMode::Overflow
                    : OverflowMode::NoOverflow;
                $anchorDay = match ($overflow) {

View on GitHub (pinned to b13f05955d)

When it happens

Trigger: Thrown at src/Carbon/Traits/Date.php:2537 when the library encounters an invalid state.

Common situations: See trigger scenarios.


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