PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, March 16, 2022

[FIXED] CakePHP 4 Date format for FormHelper->date() how to change to UK format

 March 16, 2022     cakephp, cakephp-4.x, date, forms, localization     No comments   

Issue

I have the following code in a template:

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

This displays a lovely new dynamic date picker, but with the US format "mm/dd/yyyy". What I want is "dd/mm/YYYY"

In app.php I've set the APP_DEFAULT_LOCALE to en-GB. In AppController.php I've set the following:

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

How do I change the format? I can't find anything in the docs or online.


Solution

You can't change it, at least there's no reliable, cross browser/device compatible way, the control is rendered by the browser, and the current state of things is that browsers use the locale the browser currently runs in to format the control.

If you want something solid, then you'll have to use a custom JavaScript datepicker. If you want to walk on the edge, look into web components.

See also Is there any way to change input type="date" format?.



Answered By - ndm
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing