CDateFormatter: Problem with UK date format

Hi all

I’m having a problem getting CDateFormatter to correctly interpret the UK date format (dd/MM/yyyy). It treats dates that I pass to it as US format (MM/dd/yyyy).

For example:

<?php echo Yii::app()->dateFormatter->format("yyyy-MM-dd","01/03/2010"); ?>

This produces 2010-01-03 rather than 2010-03-01.

I have set the locale in config/main.php as follows:


I have also tried constructing CDataFormatter with the locale as follows:

<?php $df = new CDateFormatter('en_gb'); ?>

However, neither of these have made a difference.

What do I need to do to get CDateFormatter to handle the UK date format correctly?

Many thanks.

It seems that this is a PHP problem rather than a Yii one.

I have however found a workaround by using date_create_from_format() which is available as of PHP 5.3:


$date = '01/03/2010';

echo date('Y-m-d',strtotime($date));                           // INCORRECT: 2010-01-03

echo date_create_from_format('d/m/Y', $date)->format('Y-m-d'); // CORRECT:   2010-03-01


It would be useful if CDateFormatter could optionally take a source date format. I will raise this as a feature request.

Issue 1059 raised: