{"record":{"id":"857eb67987cd7675","repo":"PHPOffice/PhpSpreadsheet","slug":"invalid-value-calculatedatetimetype-for-calculate","errorCode":null,"errorMessage":"Invalid value $calculateDateTimeType for calculated date time type","messagePattern":"Invalid value \\$calculateDateTimeType for calculated date time type","errorType":"exception","errorClass":"CalculationException","httpStatus":null,"severity":"error","filePath":"src/PhpSpreadsheet/Cell/Cell.php","lineNumber":405,"sourceCode":"    }\n\n    public const CALCULATE_DATE_TIME_ASIS = 0;\n    public const CALCULATE_DATE_TIME_FLOAT = 1;\n    public const CALCULATE_TIME_FLOAT = 2;\n\n    private static int $calculateDateTimeType = self::CALCULATE_DATE_TIME_ASIS;\n\n    public static function getCalculateDateTimeType(): int\n    {\n        return self::$calculateDateTimeType;\n    }\n\n    /** @throws CalculationException */\n    public static function setCalculateDateTimeType(int $calculateDateTimeType): void\n    {\n        self::$calculateDateTimeType = match ($calculateDateTimeType) {\n            self::CALCULATE_DATE_TIME_ASIS, self::CALCULATE_DATE_TIME_FLOAT, self::CALCULATE_TIME_FLOAT => $calculateDateTimeType,\n            default => throw new CalculationException(\"Invalid value $calculateDateTimeType for calculated date time type\"),\n        };\n    }\n\n    /**\n     * Convert date, time, or datetime from int to float if desired.\n     */\n    private function convertDateTimeInt(mixed $result): mixed\n    {\n        if (is_int($result)) {\n            if (self::$calculateDateTimeType === self::CALCULATE_TIME_FLOAT) {\n                if (SharedDate::isDateTime($this, $result, false)) {\n                    $result = (float) $result;\n                }\n            } elseif (self::$calculateDateTimeType === self::CALCULATE_DATE_TIME_FLOAT) {\n                if (SharedDate::isDateTime($this, $result, true)) {\n                    $result = (float) $result;\n                }\n            }","sourceCodeStart":387,"sourceCodeEnd":423,"githubUrl":"https://github.com/PHPOffice/PhpSpreadsheet/blob/65b080eef4d9fd11a5796135ab145883e5c3d6a6/src/PhpSpreadsheet/Cell/Cell.php#L387-L423","documentation":"Cell::setCalculateDateTimeType() controls whether calculated date/time results stay as-is (int) or are converted to float (datetime float or time-only float). The static setter only accepts the three CALCULATE_DATE_TIME_* constants; any other integer throws a CalculationException immediately. It is a closed enum-style setting, not an arbitrary mode number.","triggerScenarios":"Calling Cell::setCalculateDateTimeType(3) or any hardcoded number; passing a value read from env/config/DB that stores the mode as an int instead of the constant name; chaining calls where a boolean was cast to int.","commonSituations":"Copy-pasted snippets using magic numbers; settings files that predate the constants; unit tests flipping the mode via data providers that include invalid rows.","solutions":["Use the class constants: Cell::CALCULATE_DATE_TIME_ASIS, Cell::CALCULATE_DATE_TIME_FLOAT, or Cell::CALCULATE_TIME_FLOAT","Map human config strings ('as-is', 'float', 'time-float') to the constants in a single factory","Read back with Cell::getCalculateDateTimeType() to verify the mode after setting it"],"exampleFix":"// before\nCell::setCalculateDateTimeType(3); // throws CalculationException\n\n// after\nCell::setCalculateDateTimeType(Cell::CALCULATE_TIME_FLOAT);","handlingStrategy":"validation","validationCode":"const DT_MODES = [\n    'asis' => Cell::CALCULATE_DATE_TIME_ASIS,\n    'float' => Cell::CALCULATE_DATE_TIME_FLOAT,\n    'time-float' => Cell::CALCULATE_TIME_FLOAT,\n];\n$mode = $dtModes[$config['datetime_mode']] ?? Cell::CALCULATE_DATE_TIME_ASIS;\nCell::setCalculateDateTimeType($mode);","typeGuard":"function isValidDateTimeMode(int $mode): bool\n{\n    return in_array($mode, [Cell::CALCULATE_DATE_TIME_ASIS, Cell::CALCULATE_DATE_TIME_FLOAT, Cell::CALCULATE_TIME_FLOAT], true);\n}","tryCatchPattern":"null","preventionTips":["Only pass Cell::CALCULATE_DATE_TIME_* constants - the setting is a closed set","Keep mode configuration as strings and translate to constants in one place","Add the three valid values to test data providers so invalid rows fail loudly in CI"],"tags":["phpspreadsheet","calculation","datetime","invalid-enum","configuration"],"backgroundTag":"invalid-enum-value","analyzedSha":"65b080eef4d9fd11a5796135ab145883e5c3d6a6","analyzedAt":"2026-08-17T05:40:41.646Z","schemaVersion":2},"datasetVersion":"2026-08-17T09:17:11.063Z"}