Difference between #3 and #2 of Using International Dates

unchanged
Title
Using International Dates
unchanged
Category
How-tos
unchanged
Tags
i18n, date, validation rules
changed
Content
Introduction
------------------
I needed British (dd/mm/yyyy) formatted dates throughout my application but
found it very difficult to find examples of how to do this - until I found this
excellent forum post
[http://www.yiiframework.com/forum/index.php?/topic/3649-dealing-with-i18n-date-formats/](http://www.yiiframework.com/forum/index.php?/topic/3649-dealing-with-i18n-date-formats/
"http://www.yiiframework.com/forum/index.php?/topic/3649-dealing-with-i18n-date-formats/")

So here is my complete solution - note that I'm using the 'short' date format
throughout.

Set the Locale
------------------
Add this line to protected/main/config.php, I have it just after the application
name
~~~
[php]
return array(
	'basePath'=>dirname(__FILE__).DIRECTORY_SEPARATOR.'..',
	'name'=>'My Application',
	'sourceLanguage'=>'en_gb',
~~~

Set the validation rule
------------------
In your model, eg: protected/models/client.php add this format rule for any
dates

~~~
[php]
public function rules()
{
	return array(
		...
		array('date_of_birth','date','format'=>Yii::app()->locale->getDateFormat('short')),
		...
~~~

Saving dates
------------------
I've modified the above forum post so that its set for short dates. Again this
is in your model

~~~
[php]
protected function beforeSave()
{
    if(parent::beforeSave())
    {
        // Format dates based on the locale
        foreach($this->metadata->tableSchema->columns as $columnName
=> $column)
        {
            if ($column->dbType == 'date')
            {
                $this->$columnName = date('Y-m-d',
                    CDateTimeParser::parse($this->$columnName,
                    Yii::app()->locale->getDateFormat('short')));
            }
            elseif ($column->dbType == 'datetime')
            {
                $this->$columnName = date('Y-m-d H:i:s',
                    CDateTimeParser::parse($this->$columnName,
                    Yii::app()->locale->getDateTimeFormat('short')));
            }
        }
        return true;
    }
    else
        return false;
}

~~~


Formatting dates
------------------
And finally formatting the dates after a find - again this is in your model.
~~~
[php]
protected function afterFind()
{
    // Format dates based on the locale
    foreach($this->metadata->tableSchema->columns as $columnName =>
$column)
    {			
        if (!strlen($this->$columnName)) continue;

        if ($column->dbType == 'date')
        { 
            $this->$columnName =
Yii::app()->dateFormatter->formatDateTime(
                    CDateTimeParser::parse(
                        $this->$columnName, 
                        'yyyy-MM-dd'
                    ),
                    'short',null
                );
        }
        elseif ($column->dbType == 'datetime')'datetime' ||
$column->dbType == 'timestamp')
        {
            $this->$columnName =
Yii::app()->dateFormatter->formatDateTime(
                    CDateTimeParser::parse(
                        $this->$columnName, 
                        'yyyy-MM-dd hh:mm:ss'
                    ),
                    'short','short'
                );
        }
    }
    return true;parent::afterFind();
}	

~~~

Changing the target language
------------------
In theory (I haven't tested it) you should be able to change the target language
on the fly - my plan is when a user registers, the locale is set after login and
so the dates are formatted accordingly.

~~~
[php]
Yii::app()->setLanguage($lang);
~~~