PHPOffice/PhpSpreadsheet · error · CalculationException

Invalid value $calculateDateTimeType for calculated date tim

Error message

Invalid value $calculateDateTimeType for calculated date time type

What it means

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.

Source

Thrown at src/PhpSpreadsheet/Cell/Cell.php:405

    }

    public const CALCULATE_DATE_TIME_ASIS = 0;
    public const CALCULATE_DATE_TIME_FLOAT = 1;
    public const CALCULATE_TIME_FLOAT = 2;

    private static int $calculateDateTimeType = self::CALCULATE_DATE_TIME_ASIS;

    public static function getCalculateDateTimeType(): int
    {
        return self::$calculateDateTimeType;
    }

    /** @throws CalculationException */
    public static function setCalculateDateTimeType(int $calculateDateTimeType): void
    {
        self::$calculateDateTimeType = match ($calculateDateTimeType) {
            self::CALCULATE_DATE_TIME_ASIS, self::CALCULATE_DATE_TIME_FLOAT, self::CALCULATE_TIME_FLOAT => $calculateDateTimeType,
            default => throw new CalculationException("Invalid value $calculateDateTimeType for calculated date time type"),
        };
    }

    /**
     * Convert date, time, or datetime from int to float if desired.
     */
    private function convertDateTimeInt(mixed $result): mixed
    {
        if (is_int($result)) {
            if (self::$calculateDateTimeType === self::CALCULATE_TIME_FLOAT) {
                if (SharedDate::isDateTime($this, $result, false)) {
                    $result = (float) $result;
                }
            } elseif (self::$calculateDateTimeType === self::CALCULATE_DATE_TIME_FLOAT) {
                if (SharedDate::isDateTime($this, $result, true)) {
                    $result = (float) $result;
                }
            }

View on GitHub (pinned to 65b080eef4)

Solutions

  1. Use the class constants: Cell::CALCULATE_DATE_TIME_ASIS, Cell::CALCULATE_DATE_TIME_FLOAT, or Cell::CALCULATE_TIME_FLOAT
  2. Map human config strings ('as-is', 'float', 'time-float') to the constants in a single factory
  3. Read back with Cell::getCalculateDateTimeType() to verify the mode after setting it

Example fix

// before
Cell::setCalculateDateTimeType(3); // throws CalculationException

// after
Cell::setCalculateDateTimeType(Cell::CALCULATE_TIME_FLOAT);
Defensive patterns

Strategy: validation

Validate before calling

const DT_MODES = [
    'asis' => Cell::CALCULATE_DATE_TIME_ASIS,
    'float' => Cell::CALCULATE_DATE_TIME_FLOAT,
    'time-float' => Cell::CALCULATE_TIME_FLOAT,
];
$mode = $dtModes[$config['datetime_mode']] ?? Cell::CALCULATE_DATE_TIME_ASIS;
Cell::setCalculateDateTimeType($mode);

Type guard

function isValidDateTimeMode(int $mode): bool
{
    return in_array($mode, [Cell::CALCULATE_DATE_TIME_ASIS, Cell::CALCULATE_DATE_TIME_FLOAT, Cell::CALCULATE_TIME_FLOAT], true);
}

Try / catch

null

Prevention

When it happens

Trigger: 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.

Common situations: Copy-pasted snippets using magic numbers; settings files that predate the constants; unit tests flipping the mode via data providers that include invalid rows.

Related errors


AI-assisted analysis of PHPOffice/PhpSpreadsheet@65b080eef4 (2026-08-17). Data as JSON: /api/errors/857eb67987cd7675. Report an issue: GitHub.