octobercms/october · error · InvalidArgumentException

Invalid date value supplied to DateTime helper.

Error message

Invalid date value supplied to DateTime helper.

What it means

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.

Source

Thrown at modules/system/helpers/DateTime.php:83

            $value = Date::instance($value);
        }
        elseif (is_numeric($value)) {
            $value = Date::createFromTimestamp($value);
        }
        elseif (preg_match('/^(\d{4})-(\d{2})-(\d{2})$/', $value)) {
            $value = Date::createFromFormat('Y-m-d', $value)->startOfDay();
        }
        else {
            try {
                $value = Date::parse($value);
            }
            catch (Exception $ex) {
                // Do nothing
            }
        }

        if (!$value instanceof Carbon && $throwException) {
            throw new InvalidArgumentException('Invalid date value supplied to DateTime helper.');
        }

        return $value;
    }

    /**
     * momentFormat converts a PHP date format to "Moment.js" format.
     * @param string $format
     * @return string
     */
    public static function momentFormat($format)
    {
        if (!$format) {
            return '';
        }

        $replacements = [
            'd' => 'DD',

View on GitHub (pinned to b608633a7e)

Solutions

  1. Validate/normalize the input before it reaches date helpers (add a date rule to the form field)
  2. Parse known formats explicitly: Carbon::createFromFormat('d/m/Y', $value)
  3. Pass null instead of '' for empty values, since null is not fed to the parser
  4. For lenient contexts call makeCarbon($value, throwException: false), which returns the original value instead of throwing

Example fix

// before
$date = \System\Helpers\DateTime::makeCarbon('31/02/2023');

// after
try {
    $date = \System\Helpers\DateTime::makeCarbon($input);
} catch (\InvalidArgumentException $ex) {
    $date = \Carbon\Carbon::createFromFormat('d/m/Y', $input); // or reject the input
}
Defensive patterns

Strategy: type-guard

Validate before calling

if (is_string($value)) { $value = trim($value) ?: null; }
$date = \System\Helpers\DateTime::makeCarbon($value, throwException: false);
if (!$date instanceof \Carbon\Carbon) {
    // reject or re-parse with an explicit format before continuing
}

Type guard

function isParseableDate($value): bool
{
    if ($value instanceof \Carbon\CarbonInterface || $value instanceof \DateTime) {
        return true;
    }
    if (is_int($value)) {
        return true;
    }
    if (!is_string($value) || $value === '') {
        return false;
    }
    try { \Carbon\Carbon::parse($value); return true; } catch (\Throwable) { return false; }
}

Try / catch

try { $date = \System\Helpers\DateTime::makeCarbon($input); } catch (\InvalidArgumentException $ex) { // reject the raw input or fall back to Carbon::createFromFormat with the known format }

Prevention

When it happens

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

Common situations: Form fields missing a date validation rule; database columns storing '' rather than NULL; integrations feeding locale-specific formats into helpers that ultimately call makeCarbon.

Related errors


AI-assisted analysis of octobercms/october@b608633a7e (2026-08-21). Data as JSON: /api/errors/5df15bbe6abaa103. Report an issue: GitHub.