{"record":{"id":"de1a914d8ea79b8f","repo":"PHPOffice/PhpSpreadsheet","slug":"num-de1a91","errorCode":"#NUM!","errorMessage":"#NUM!","messagePattern":"#NUM!","errorType":"exception","errorClass":"PhpOffice\\PhpSpreadsheet\\Calculation\\Exception","httpStatus":null,"severity":"error","filePath":"src/PhpSpreadsheet/Calculation/DateTimeExcel/Week.php","lineNumber":199,"sourceCode":"                $DoW = self::dow0Becomes7($DoW) - 1;\n\n                break;\n        }\n\n        return $DoW;\n    }\n\n    /**\n     * @param mixed $style expect int\n     */\n    private static function validateStyle(mixed $style): int\n    {\n        if (!is_numeric($style)) {\n            throw new Exception(ExcelError::VALUE());\n        }\n        $style = (int) $style;\n        if (($style < 1) || ($style > 3)) {\n            throw new Exception(ExcelError::NAN());\n        }\n\n        return $style;\n    }\n\n    private static function dow0Becomes7(int $DoW): int\n    {\n        return ($DoW === 0) ? 7 : $DoW;\n    }\n\n    /**\n     * @param mixed $dateValue Excel date serial value (float), PHP date timestamp (integer),\n     *                                    PHP DateTime object, or a standard date string\n     */\n    private static function apparentBug(mixed $dateValue): bool\n    {\n        if (SharedDateHelper::getExcelCalendar() !== SharedDateHelper::CALENDAR_MAC_1904) {\n            if (is_bool($dateValue)) {","sourceCodeStart":181,"sourceCodeEnd":217,"githubUrl":"https://github.com/PHPOffice/PhpSpreadsheet/blob/65b080eef4d9fd11a5796135ab145883e5c3d6a6/src/PhpSpreadsheet/Calculation/DateTimeExcel/Week.php#L181-L217","documentation":"PhpSpreadsheet throws this when the second (style / return-type) argument of the Excel WEEKDAY function is numeric but outside the allowed 1-3 range. validateStyle() in DateTimeExcel\\Week casts the value to int and any result below 1 or above 3 throws Calculation\\Exception('#NUM!'); the public Week::day() wrapper catches it and returns the string '#NUM!' as the cell/result value. Excel itself only supports styles 1 (Sunday=1..7), 2 (Monday=1..7) and 3 (Monday=0..6).","triggerScenarios":"Calling =WEEKDAY(A1, 4), =WEEKDAY(A1, 0) or =WEEKDAY(A1, 11) in a worksheet; calling Week::day($dateValue, 11) or passing an array of styles containing an out-of-range entry. Numeric strings like '5' are cast to int and still fail the 1-3 check; only non-numeric styles give #VALUE! instead.","commonSituations":"Confusing WEEKDAY's style with WEEKNUM's method codes (which do accept 11-17 and 21) and passing 11-17 to WEEKDAY; expecting 0 to be a valid zero-based style; formulas imported from other spreadsheet tools or assembled by string concatenation with a wrong constant.","solutions":["Set the WEEKDAY style to 1, 2 or 3 (e.g. =WEEKDAY(A1, 2)); no other value is supported.","If you wanted a week-start method like 11-17 or 21, that belongs to WEEKNUM: use =WEEKNUM(date, method) / Week::number($date, 21), or ISOWEEKNUM for ISO weeks.","Sanitize user-supplied style values before building the formula (cast to int and clamp/reject outside 1-3).","When evaluating untrusted formulas, detect the failure by checking the calculated result for the literal string '#NUM!'."],"exampleFix":"// before - returns '#NUM!'\n$value = \\PhpOffice\\PhpSpreadsheet\\Calculation\\DateTimeExcel\\Week::day('2026-08-16', 11);\n\n// after - style 2: Monday=1 .. Sunday=7\n$value = \\PhpOffice\\PhpSpreadsheet\\Calculation\\DateTimeExcel\\Week::day('2026-08-16', 2);","handlingStrategy":"validation","validationCode":"$style = (int) $style;\nif ($style < 1 || $style > 3) {\n    throw new \\InvalidArgumentException('WEEKDAY style must be 1, 2 or 3, got ' . $style);\n}\n$value = \\PhpOffice\\PhpSpreadsheet\\Calculation\\DateTimeExcel\\Week::day($dateValue, $style);","typeGuard":"/** WEEKDAY only accepts styles 1, 2 or 3. */\nfunction isValidWeekdayStyle(mixed $style): bool\n{\n    return is_numeric($style) && (int) $style >= 1 && (int) $style <= 3;\n}","tryCatchPattern":null,"preventionTips":["Keep a project constant set for the three styles and never pass WEEKNUM method codes (11-17, 21) to WEEKDAY.","Assert isValidWeekdayStyle() before building =WEEKDAY() formulas from user input.","Treat a calculated '#NUM!' string as a signal the style argument was out of range."],"tags":["phpspreadsheet","excel-formula","weekday","date-function","argument-validation"],"backgroundTag":"excel-num-error","analyzedSha":"65b080eef4d9fd11a5796135ab145883e5c3d6a6","analyzedAt":"2026-08-17T05:40:41.646Z","schemaVersion":2},"datasetVersion":"2026-08-17T09:17:11.063Z"}