Yii - Date transformations methods

Hello again,

Can I please ask your help in order to find the proper method to use on this workflow:



The user inserts a date using the following format: dd/mm/yyyy

That date is inserted into the mySQL table date field type as: yyyy/mm/dd

So, and please correct me if I’m wrong, we need to transform the date format from dd/mm/yyyy to [i]yyyy/mm/dd


Question 1)

What yii method (if any) should we use ?


When we display the date back to the user we should displayed in two ways according to the model or controller in question:


On an update form field or any other edit operation, it should appear as:


Question 2.1)

I believe this would be the same method as used in 1 ?


On other views it should:

[size="3"]2.2.1)[/size] split the day and the month.

Question 2.2.1

What method can we use to split it ?

[size="3"] 2.2.2)[/size]

The day should be in numbers;

[size="3"] 2.2.3)[/size]

The month should be full name (ie. January) but translated (ie. Janvier)

Question 2.2.3

What method can we use to, from the above, return the full month name on a given


I don’t need a full example with all the job done but, at least a finger pointer to specific methods then would help me have the job done please.

Thanks in advance,


before u save use like this


1st issue solved.

to display this date use reverse method.

use date(‘Y’,strtotime($model->ur-attribute-date)); - for Year

like for month date(‘m’,strtotime($model->ur-attribute-date));

if u need to translate, use check for translate components and add t() function.

Hi Rajith,

I need to compare 2 dates: one taken from mysql table (so in the format yyyy-mm-dd) and the other one is a string retrieved from an input in a form but the format is still yyyy-mm-dd. How do I compare the 2 dates?




this u can convert it to any format … and compare simply using ’ =, <, > ’ …

use like this






Thanks Rajith,

My problem is that I am using it in the model->find() method and here I cannot use strtotime since it is php. How to solve this?

so write as criteria

$criteria->condition=$criteria->condition.' and '.'date_of_birth > :date_of_birth';

$criteria->params[':date_of_birth'] = date('Y-m-d',strtotime($_REQUEST['Students']['date_of_birth']));

so u just need to check equal to ?? right… so change one others format… using strtotime…then use in find()…

@Jimlam: Please don’t exploit others post. Create your own post for your own doubts. Things get clear this way.


To retrieve the day and the month I’ve used two methods on the model:


         * @desc Given a string, '2012-12-02' returns the day: '02'

         * @param string $date

         * @return string 


        public function getDay($date){            

            return yii::app()->dateFormatter->format('dd', $date);




         * @desc Given a string, '2012-12-02' returns the month: 'December'

         * @param string $date

         * @return string 


        public function getMounth($date){            

            return yii::app()->dateFormatter->format('MMMM', $date);


The date is inserted into the database as yyyy-mm-dd like this:

Let’s say: 06/11/2016

protected function beforeSave ()




    list($d, $m, $y) = explode('/', $this->date);

    $mk=mktime(0, 0, 0, $m, $d, $y);

    $this->date = date ('Y-m-d', $mk);


 return parent::beforeSave ();


On my form (update) I’m getting this:


How can I fix this ?

@mem: Sorry, I meant no harm. Since I was having a sort of similar problem, I thought it could be beneficial to all those concerned. Anyway, I have up to now noticed a very friendly and helpful attitude from all those who contribute to this community. My sincere apologies for having ‘exploited’ your post.

BTW, I think your problem could be solved by including in your model an afterFind() method, thus:

	protected function afterFind ()


  		if($this->date <> '')


  				// mise en forme de date

  				list($y, $m, $d) = explode('/', $this->date);

  				$mk=mktime(0, 0, 0, $m, $d, $y);

  				$this->date = date ('d/m/Y', $mk);

  	return parent::afterFind ();


Hope it helps