cakephp/cakephp · error · InvalidArgumentException

UNIT_TIMESTAMP_FORMAT is not supported for Date.

Error message

UNIT_TIMESTAMP_FORMAT is not supported for Date.

What it means

Cake\I18n\Date represents a calendar date with no time component, so i18nFormat() cannot render it as a UNIX timestamp. Passing DateTime::UNIX_TIMESTAMP_FORMAT as the format throws InvalidArgumentException instead of returning a meaningless integer.

Solutions

  1. Use an IntlDateFormatter constant or format string instead of UNIX_TIMESTAMP_FORMAT.
  2. Use $date->format('U') when a timestamp of a Date is explicitly needed.
  3. Audit static::$_toStringFormat / serialization config so Date instances never receive UNIX_TIMESTAMP_FORMAT.

Example fix

// before
$date->i18nFormat(Date::UNIX_TIMESTAMP_FORMAT);
// after
$date->format('U'); // or $date->i18nFormat(IntlDateFormatter::SHORT)
Defensive patterns

Strategy: validation

Validate before calling

if ($format === \Cake\I18n\DateTime::UNIX_TIMESTAMP_FORMAT && $date instanceof \Cake\I18n\Date) {
    $format = IntlDateFormatter::SHORT; // Date has no time component
}
$out = $date->i18nFormat($format);

Type guard

function dateSupportsFormat(object $date, mixed $format): bool {
    return !($date instanceof \Cake\I18n\Date && $format === \Cake\I18n\DateTime::UNIX_TIMESTAMP_FORMAT);
}

Try / catch

try {
    $out = $date->i18nFormat($format);
} catch (InvalidArgumentException $e) {
    $out = $date->format('U');
}

Prevention

When it happens

Trigger: Calling (new Date(...))->i18nFormat(Date::UNIX_TIMESTAMP_FORMAT), calling nice() with a timestamp format, setting static::$_toStringFormat to UNIX_TIMESTAMP_FORMAT so jsonSerialize()/__toString() hit the guard.

Common situations: Reusing formatting code written for Time/DateTime on a Date object; globally configuring serialization formats as UNIX_TIMESTAMP_FORMAT and then serializing Date-only model fields.

Understand the failure class

Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.

Related errors


AI-assisted analysis of cakephp/cakephp@1128eba9b0 (2026-09-12). Data as JSON: /api/errors/8d8f7df40029a598. Report an issue: GitHub.

Appendix: source

Thrown at src/I18n/Date.php:244

     * ```
     * $date = new Date('2014-04-20');
     * $time->i18nFormat(null, 'de-DE');
     * $time->i18nFormat(\IntlDateFormatter::FULL, 'de-DE');
     * ```
     *
     * You can control the default locale used through `Date::setDefaultLocale()`.
     * If empty, the default will be taken from the `intl.default_locale` ini config.
     *
     * @param string|int|null $format Format string.
     * @param string|null $locale The locale name in which the date should be displayed (e.g. pt-BR)
     * @return string|int Formatted and translated date string
     */
    public function i18nFormat(
        string|int|null $format = null,
        ?string $locale = null,
    ): string|int {
        if ($format === DateTime::UNIX_TIMESTAMP_FORMAT) {
            throw new InvalidArgumentException('UNIT_TIMESTAMP_FORMAT is not supported for Date.');
        }

        $format ??= static::$_toStringFormat;
        $format = is_int($format) ? [$format, IntlDateFormatter::NONE] : $format;
        $locale = $locale ?: DateTime::getDefaultLocale();

        return $this->_formatObject($this->native, $format, $locale);
    }

    /**
     * Returns a nicely formatted date string for this object.
     *
     * The format to be used is stored in the static property `Date::$niceFormat`.
     *
     * @param string|null $locale The locale name in which the date should be displayed (e.g. pt-BR)
     * @return string Formatted date string
     */
    public function nice(?string $locale = null): string

View on GitHub (pinned to 1128eba9b0)