{"record":{"id":"5df15bbe6abaa103","repo":"octobercms/october","slug":"invalid-date-value-supplied-to-datetime-helper","errorCode":null,"errorMessage":"Invalid date value supplied to DateTime helper.","messagePattern":"Invalid date value supplied to DateTime helper\\.","errorType":"exception","errorClass":"InvalidArgumentException","httpStatus":null,"severity":"error","filePath":"modules/system/helpers/DateTime.php","lineNumber":83,"sourceCode":"            $value = Date::instance($value);\n        }\n        elseif (is_numeric($value)) {\n            $value = Date::createFromTimestamp($value);\n        }\n        elseif (preg_match('/^(\\d{4})-(\\d{2})-(\\d{2})$/', $value)) {\n            $value = Date::createFromFormat('Y-m-d', $value)->startOfDay();\n        }\n        else {\n            try {\n                $value = Date::parse($value);\n            }\n            catch (Exception $ex) {\n                // Do nothing\n            }\n        }\n\n        if (!$value instanceof Carbon && $throwException) {\n            throw new InvalidArgumentException('Invalid date value supplied to DateTime helper.');\n        }\n\n        return $value;\n    }\n\n    /**\n     * momentFormat converts a PHP date format to \"Moment.js\" format.\n     * @param string $format\n     * @return string\n     */\n    public static function momentFormat($format)\n    {\n        if (!$format) {\n            return '';\n        }\n\n        $replacements = [\n            'd' => 'DD',","sourceCodeStart":65,"sourceCodeEnd":101,"githubUrl":"https://github.com/octobercms/october/blob/b608633a7e8922487d91a8161499020121c3b3bf/modules/system/helpers/DateTime.php#L65-L101","documentation":"System\\Helpers\\DateTime::makeCarbon($value, $throwException = true) normalizes mixed input to Carbon: Carbon and DateTime instances pass through, integers become unix timestamps (Date::createFromTimestamp), strings matching Y-m-d parse at startOfDay, and anything else is attempted with Date::parse (Carbon::parse) with exceptions swallowed. If the final value still is not a Carbon instance and $throwException is true, InvalidArgumentException 'Invalid date value supplied to DateTime helper.' is thrown.","triggerScenarios":"Passing a non-empty string Carbon cannot parse ('31/02/2023', '12 noon', 'not-a-date') so Date::parse throws silently and $value stays a string; an empty string from a DB column instead of NULL; unvalidated user input from a date field; locale-formatted dd/mm/yyyy dates that match neither the strict Y-m-d regex nor Carbon's parser.","commonSituations":"Form fields missing a date validation rule; database columns storing '' rather than NULL; integrations feeding locale-specific formats into helpers that ultimately call makeCarbon.","solutions":["Validate/normalize the input before it reaches date helpers (add a date rule to the form field)","Parse known formats explicitly: Carbon::createFromFormat('d/m/Y', $value)","Pass null instead of '' for empty values, since null is not fed to the parser","For lenient contexts call makeCarbon($value, throwException: false), which returns the original value instead of throwing"],"exampleFix":"// before\n$date = \\System\\Helpers\\DateTime::makeCarbon('31/02/2023');\n\n// after\ntry {\n    $date = \\System\\Helpers\\DateTime::makeCarbon($input);\n} catch (\\InvalidArgumentException $ex) {\n    $date = \\Carbon\\Carbon::createFromFormat('d/m/Y', $input); // or reject the input\n}","handlingStrategy":"type-guard","validationCode":"if (is_string($value)) { $value = trim($value) ?: null; }\n$date = \\System\\Helpers\\DateTime::makeCarbon($value, throwException: false);\nif (!$date instanceof \\Carbon\\Carbon) {\n    // reject or re-parse with an explicit format before continuing\n}","typeGuard":"function isParseableDate($value): bool\n{\n    if ($value instanceof \\Carbon\\CarbonInterface || $value instanceof \\DateTime) {\n        return true;\n    }\n    if (is_int($value)) {\n        return true;\n    }\n    if (!is_string($value) || $value === '') {\n        return false;\n    }\n    try { \\Carbon\\Carbon::parse($value); return true; } catch (\\Throwable) { return false; }\n}","tryCatchPattern":"try { $date = \\System\\Helpers\\DateTime::makeCarbon($input); } catch (\\InvalidArgumentException $ex) { // reject the raw input or fall back to Carbon::createFromFormat with the known format }","preventionTips":["Add date validation rules to form fields that feed date helpers","Store NULL, not empty strings, for missing datetime values","Parse locale-specific formats explicitly with Carbon::createFromFormat"],"tags":["date","carbon","parsing","validation"],"backgroundTag":"date-parse-error","analyzedSha":"b608633a7e8922487d91a8161499020121c3b3bf","analyzedAt":"2026-08-21T04:24:57.515Z","schemaVersion":2},"datasetVersion":"2026-08-21T11:28:35.574Z"}