Using International Dates

You are viewing revision #2 of this wiki article.
This version may not be up to date with the latest version.
You may want to view the differences to the latest version or see the changes made in this revision.

« previous (#1)next (#3) »

  1. Introduction
  2. Set the Locale
  3. Set the validation rule
  4. Saving dates
  5. Formatting dates
  6. Changing the target language

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/

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

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

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

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.

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')
        {
            $this->$columnName = Yii::app()->dateFormatter->formatDateTime(
                    CDateTimeParser::parse(
                        $this->$columnName, 
                        'yyyy-MM-dd hh:mm:ss'
                    ),
                    'short','short'
                );
        }
    }
    return true;
}	

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.

Yii::app()->setLanguage($lang);
15 0
24 followers
Viewed: 49 862 times
Version: Unknown (update)
Category: How-tos
Written by: Russell England
Last updated by: nkd
Created on: Apr 28, 2011
Last updated: 12 years ago
Update Article

Revisions

View all history

Related Articles