Use one date format in the whole cakephp 3.x project

This may be a simple question.

Is there a way that I can set in my project in only one date format and have this date format all over the world. And everywhere I mean viewing, editing, index action. The format I need is - Y-m-d H:i:sand it has nothing to do with my locale date format.

I tried using the following code inside bootstrap file but it did not work

Type::build('date')->useLocaleParser()->setLocaleFormat('Y-m-d');
Type::build('datetime')->useLocaleParser()->setLocaleFormat('Y-m-d H:i');

In the index, he showed the date as 10/10/15, 8:20 PM.

Also for editing the date I use text input, so I need them in the text box to display the date as 2015/10/10 20:20.

+4
source share
2 answers

CakePHP has a number of useful classes and associates helpers who will do most of the repetitive or difficult work for you.

In this case, the utility class Timecan take care of building globally if you install it with the standard format that you need.

+2
source

I recommend that you create a file / class that serves to place the values ​​of the CONST application, and then require that this file / autoload of this class in the file of the application boot file.

define("MYSQL_DATETIME_FORMAT", "Y-m-d H:i:s");

Then define a property in your ApplicationController, for example $mysql_datetime = MYSQL_DATETIME_FORMAT;, and pass that value to your views, view helper, or decorator class.

(.. ), (, function to_mysql_datetime($timeval){...}). :

$formatted_datetime = DateFormatHelper::to_mysql_datetime($other_datetime_value);

CakePHP :

, config/bootstrap.php. CLI.

:

. . . . .

MVC : , . youll . " ", , .

-1

Source: https://habr.com/ru/post/1615121/


All Articles