CakePHP 4日期格式为FormHelper-> date()如何更改为英国格式

时间:2020-01-10 12:41:49

标签: forms date cakephp localization cakephp-4.x

我在模板中有以下代码:

        <?= $this->Form->date('selected_date', ['required' => true]) ?>

这将显示一个可爱的新动态日期选择器,但美国格式为“ mm / dd / yyyy”。我想要的是“ dd / mm / YYYY”

在app.php中,我已将APP_DEFAULT_LOCALE设置为en-GB。 在AppController.php中,我设置了以下内容:

I18n::setLocale('en-GB');   
Time::setDefaultLocale('en-GB'); // For any mutable DateTime
FrozenTime::setDefaultLocale('en-GB'); // For any immutable DateTime
Date::setDefaultLocale('en-GB'); // For any mutable Date
FrozenDate::setDefaultLocale('en-GB'); // For any immutable Date

如何更改格式?我在文档或在线中找不到任何内容。

1 个答案:

答案 0 :(得分:1)

您无法更改它,至少没有可靠的,跨浏览器/设备兼容的方式,控件由浏览器呈现,并且当前状态是浏览器使用浏览器当前运行的语言环境进行格式化控件。

如果您想要一些坚固的东西,则必须使用自定义JavaScript日期选择器。如果您想走在边缘,请查看web components

另请参见 Is there any way to change input type="date" format?