Answer the question
In order to leave comments, you need to log in
What is the best way to organize date formatting in yii and at what stages to do it?
There is a sql server with its datetime format 2014-01-13 11:40:34.423
There is a need to edit the date and time in some forms and save the changes.
The user needs to give the human date and time format 01/13/2014 and 11:40:34, and save it in the database in datetime format.
What is the best way to organize date formatting and at what stages to do it?
Answer the question
In order to leave comments, you need to log in
I store int.
When entering into fields when creating a new entity or searching / filtering a human date (Ymd) in beforeValidate, I translate to unixtime.
When outputting unixtime, I translate to Ymd
public function beforeValidate()
{
$attrs = [ 'time', 'testing', 'call' ];
foreach ( $attrs as $attr ) {
$time = \DateTime::createFromFormat( 'Y-m-d H:i:s', $this->$attr );
if ( !$time ) continue;
$this->$attr = $time->format( 'U' );
}
parent::beforeValidate();
return true;
}
There is CDateFormatter .
But if DateTime is enough for you, then there is nothing criminal in using it. Theoretically, this will complicate design changes and localization, but in practice, everything still has to be shoveled.
Of course, it is better to do date conversion in the template, where it is displayed.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question