Reputation: 2277
This might be a simple question.
Is there any way that I can set in my project just a single date format and have this date format everywhere. And by everywhere I mean the view, edit, index actions. The format I need to have is Y-m-d H:i:s
and it has nothing to do with my locale date format.
I tried to use the following code inside bootstrap file but it didn't work
Type::build('date')->useLocaleParser()->setLocaleFormat('Y-m-d');
Type::build('datetime')->useLocaleParser()->setLocaleFormat('Y-m-d H:i');
In the index it kept showing the date as 10/10/15, 8:20 PM
.
Also for editing a date I use a text input so I need them to textbox to display the date as 2015/10/10 20:20
.
Upvotes: 4
Views: 446
Reputation: 1852
CakePHP has a number of utility classes and associate helpers that will do most of the repetitive or tough work for you.
In this case the Time
utility class can take care of formating globally, if you set it up with the default format you need.
Upvotes: 2
Reputation: 3113
I recommend you create a file/class which serves to house application CONST values, and then require that file/autoload that class in your application bootstrap file.
define("MYSQL_DATETIME_FORMAT", "Y-m-d H:i:s");
Then, define a property in your ApplicationController, such as $mysql_datetime = MYSQL_DATETIME_FORMAT;
, and pass this value into your views, a view helper, or a decorator class.
You can also create a user defined or class defined callback (i.e., a helper) which uses this value and performs conversions for you (such as function to_mysql_datetime($timeval){...}
. Then in your views, you could call:
$formatted_datetime = DateFormatHelper::to_mysql_datetime($other_datetime_value);
This is from the CakePHP documentation regarding this issue:
If you have any additional configuration needs, you should add them to your application’s config/bootstrap.php file. This file is included before each request, and CLI command.
This file is ideal for a number of common bootstrapping tasks:
Defining convenience functions. Declaring constants. Creating cache configurations. Configuring inflections. Loading configuration files.
Be careful to maintain the MVC software design pattern when you add things to the bootstrap file: it might be tempting to place formatting functions there in order to use them in your controllers. As you’ll see in the Controllers and Views sections there are better ways you add custom logic to your application.
Upvotes: 0