{"record":{"id":"8d8f7df40029a598","repo":"cakephp/cakephp","slug":"unit-timestamp-format-is-not-supported-for-date","errorCode":null,"errorMessage":"UNIT_TIMESTAMP_FORMAT is not supported for Date.","messagePattern":"UNIT_TIMESTAMP_FORMAT is not supported for Date\\.","errorType":"exception","errorClass":"InvalidArgumentException","httpStatus":null,"severity":"error","filePath":"src/I18n/Date.php","lineNumber":244,"sourceCode":"     * ```\n     * $date = new Date('2014-04-20');\n     * $time->i18nFormat(null, 'de-DE');\n     * $time->i18nFormat(\\IntlDateFormatter::FULL, 'de-DE');\n     * ```\n     *\n     * You can control the default locale used through `Date::setDefaultLocale()`.\n     * If empty, the default will be taken from the `intl.default_locale` ini config.\n     *\n     * @param string|int|null $format Format string.\n     * @param string|null $locale The locale name in which the date should be displayed (e.g. pt-BR)\n     * @return string|int Formatted and translated date string\n     */\n    public function i18nFormat(\n        string|int|null $format = null,\n        ?string $locale = null,\n    ): string|int {\n        if ($format === DateTime::UNIX_TIMESTAMP_FORMAT) {\n            throw new InvalidArgumentException('UNIT_TIMESTAMP_FORMAT is not supported for Date.');\n        }\n\n        $format ??= static::$_toStringFormat;\n        $format = is_int($format) ? [$format, IntlDateFormatter::NONE] : $format;\n        $locale = $locale ?: DateTime::getDefaultLocale();\n\n        return $this->_formatObject($this->native, $format, $locale);\n    }\n\n    /**\n     * Returns a nicely formatted date string for this object.\n     *\n     * The format to be used is stored in the static property `Date::$niceFormat`.\n     *\n     * @param string|null $locale The locale name in which the date should be displayed (e.g. pt-BR)\n     * @return string Formatted date string\n     */\n    public function nice(?string $locale = null): string","sourceCodeStart":226,"sourceCodeEnd":262,"githubUrl":"https://github.com/cakephp/cakephp/blob/1128eba9b09f1946df684811350e26f2cef68fac/src/I18n/Date.php#L226-L262","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Use an IntlDateFormatter constant or format string instead of UNIX_TIMESTAMP_FORMAT.","Use $date->format('U') when a timestamp of a Date is explicitly needed.","Audit static::$_toStringFormat / serialization config so Date instances never receive UNIX_TIMESTAMP_FORMAT."],"exampleFix":"// before\n$date->i18nFormat(Date::UNIX_TIMESTAMP_FORMAT);\n// after\n$date->format('U'); // or $date->i18nFormat(IntlDateFormatter::SHORT)","handlingStrategy":"validation","validationCode":"if ($format === \\Cake\\I18n\\DateTime::UNIX_TIMESTAMP_FORMAT && $date instanceof \\Cake\\I18n\\Date) {\n    $format = IntlDateFormatter::SHORT; // Date has no time component\n}\n$out = $date->i18nFormat($format);","typeGuard":"function dateSupportsFormat(object $date, mixed $format): bool {\n    return !($date instanceof \\Cake\\I18n\\Date && $format === \\Cake\\I18n\\DateTime::UNIX_TIMESTAMP_FORMAT);\n}","tryCatchPattern":"try {\n    $out = $date->i18nFormat($format);\n} catch (InvalidArgumentException $e) {\n    $out = $date->format('U');\n}","preventionTips":["Never configure UNIX_TIMESTAMP_FORMAT for Date-typed columns/fields.","Audit global $_toStringFormat and serialization formats for Date vs DateTime separation.","Use format('U') when a timestamp of a Date is explicitly needed."],"tags":["php","cakephp","i18n","unsupported-format"],"backgroundTag":"unsupported-operation","analyzedSha":"1128eba9b09f1946df684811350e26f2cef68fac","analyzedAt":"2026-09-12T12:07:00.388Z","contentChangedAt":"2026-09-12T12:07:00.388Z","schemaVersion":2},"datasetVersion":"2026-09-19T12:17:13.211Z"}