Date format

I’ve searched other the forum for date formatting examples.

I’ve found some examples and extensions like DateTimeI18NBehavior and FormatDateTimeBehavior. All this stuff looks great but i can’t manage to get it work.

so i write this simple code to test the basic idea :

I set language in config/main.php :




$dateToLocale=Yii::app()->dateFormatter->formatDateTime(CDateTimeParser::parse($date, 'yyyy-MM-dd'),'medium',null);

$dateBackToMySQL=date('Y-m-d', CDateTimeParser::parse($dateToLocale, Yii::app()->locale->dateFormat));

echo 'My date: '.$date.'<br/>';

echo 'Yii::app()->locale->dateFormat = '.Yii::app()->locale->dateFormat.'<br/>';

echo 'My date to locale: '.$dateToLocale.'<br/>';

echo 'My date back to MySQL format: '.$dateBackToMySQL.'<br/>';

Result is :

My date: 2012-02-15

Yii::app()->locale->dateFormat = d MMM y

My date to locale: 15 févr. 2012

My date back to MySQL format: 1970-01-01

After i had a look at CDateTimeParser documentation it seems to be a normal result as CDateTimeParser does not understand pattern MMM.

But as Yii::app()->dateFormatter->formatDateTime used with ‘medium’ date width returns a date according to the pattern d MMM y, i don’t understand how this example and the above extensions mentionned can work. and it seems it should work according to several posts on the forum.

any idea?


check above link.

I think your pattern is wrong (d MMM y ) this replace with pattern d MM y.


thanks for the link !

so if i understand i have to override the initial patterns related to long, medium,… with one that will be understood by CDateTimeParser?

So that mean that it is now not possible to work with date like : 13 févr. 2012 ?

I mean transform my MySQL date to a format like 13 févr. 2012 and get it back to MySQL format before save action.


I have exactly the same question some months later, can someone please clarify it?