{"record":{"id":"020afe5b49d08323","repo":"briannesbitt/Carbon","slug":"you-cannot-set-unit-to-a-float-value-as-name","errorCode":null,"errorMessage":"You cannot set {$unit} to a float value as {$name} would be overridden, set it first to 0 explicitly if you really want to erase its value","messagePattern":"You cannot set (.+?) to a float value as (.+?) would be overridden, set it first to 0 explicitly if you really want to erase its value","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"src/Carbon/CarbonInterval.php","lineNumber":3537,"sourceCode":"            $units = [\n                'y' => 'year',\n                'm' => 'month',\n                'd' => 'day',\n                'h' => 'hour',\n                'i' => 'minute',\n                's' => 'second',\n            ];\n            $upper = true;\n\n            foreach ($units as $property => $name) {\n                if ($name === $unit) {\n                    $upper = false;\n\n                    continue;\n                }\n\n                if (!$upper && $this->$property !== 0) {\n                    throw new RuntimeException(\n                        \"You cannot set $unit to a float value as $name would be overridden, \".\n                        'set it first to 0 explicitly if you really want to erase its value'\n                    );\n                }\n            }\n\n            $this->add($unit, $floatValue - $base);\n        }\n    }\n\n    private function getInnerValues(): array\n    {\n        return [$this->y, $this->m, $this->d, $this->h, $this->i, $this->s, $this->f, $this->invert, $this->days];\n    }\n\n    private function checkStartAndEnd(): void\n    {\n        if (","sourceCodeStart":3519,"sourceCodeEnd":3555,"githubUrl":"https://github.com/briannesbitt/Carbon/blob/b13f05955dcfd7da71d60af745fd148cbdb73505/src/Carbon/CarbonInterval.php#L3519-L3555","documentation":"Since Carbon 3 (opt-in earlier via CarbonInterval::enableFloatSetters()), giving a CarbonInterval unit a decimal value (e.g. ->hours(1.5)) cascades the fractional remainder into the smaller units (minutes, seconds). handleDecimalPart() (src/Carbon/CarbonInterval.php:3509) walks the units y/m/d/h/i/s and refuses the cascade if any unit BELOW the one being set already holds a non-zero value, because writing the fraction would silently overwrite that stored amount.","triggerScenarios":"Calling a unit setter with a real decimal part on an interval whose smaller units are already populated: CarbonInterval::make('PT1H30M')->hours(2.5) (minutes=30 would be overridden), CarbonInterval::create(0, 0, 0, 1, 30)->hours(1.5), or ->years(1.5) on an interval that already has months/days set. Only fires when float setters are enabled and the value is not a whole number.","commonSituations":"Upgrading Carbon 2 to 3 where floats used to be silently truncated and now cascade; building intervals from user input containing decimals; setting smaller units before larger ones when the larger one is fractional; re-using an interval parsed from an ISO spec string and then assigning a float to an upper unit.","solutions":["Zero out the smaller units first, as the message says: $interval->minutes(0)->seconds(0)->hours(1.5)","Express the amount in the smallest affected unit instead: CarbonInterval::minutes(90) or CarbonInterval::seconds(5400)","Build from an explicit spec: CarbonInterval::make('PT2H30M')","When migrating Carbon 2 code that relied on truncation, cast explicitly: $interval->hours((int) $value)"],"exampleFix":"// before\n$interval = CarbonInterval::make('PT1H30M');\n$interval->hours(2.5); // RuntimeException: minutes would be overridden\n\n// after\n$interval = CarbonInterval::make('PT1H30M')\n    ->minutes(0)\n    ->seconds(0);\n$interval->hours(2.5); // 2h 30m, cascade lands on zeroed minutes\n\n// or simply\n$interval = CarbonInterval::minutes(150);","handlingStrategy":"validation","validationCode":"// $unit: 'year'|'month'|'day'|'hour'|'minute'|'second' (the unit you are about to set)\n$props = ['year' => 'y', 'month' => 'm', 'day' => 'd', 'hour' => 'h', 'minute' => 'i', 'second' => 's'];\n$order = array_values($props);\n$pos = array_search($props[$unit], $order, true);\nfor ($i = $pos + 1; $i < \\count($order); $i++) {\n    if ($interval->{$order[$i]} !== 0) {\n        $interval = $interval->{$order[$i]} === $order[$i] ? $interval : $interval; // no-op guard\n        throw new RuntimeException(\"Zero the {$order[$i]} unit before setting $unit to a float\");\n    }\n}\n$interval = $interval->{$unit.'s'}(1.5);","typeGuard":null,"tryCatchPattern":"try {\n    $interval->hours(1.5);\n} catch (\\RuntimeException $e) {\n    // message contains 'would be overridden': rebuild from a total instead\n    $interval = CarbonInterval::minutes((int) round($interval->totalMinutes));\n}","preventionTips":["Set larger units before smaller ones when any value is fractional","Prefer building intervals from ISO spec strings or the smallest target unit over mixing decimals into populated intervals","When migrating from Carbon 2, audit unit setters for float arguments: they no longer truncate silently"],"tags":["carbon","carbon-interval","float","duration","unit-cascade"],"backgroundTag":"duration-unit-overflow","analyzedSha":"b13f05955dcfd7da71d60af745fd148cbdb73505","analyzedAt":"2026-08-17T04:40:56.953Z","schemaVersion":2},"datasetVersion":"2026-08-17T09:17:11.063Z"}