Difference between #1 and #3 of
Using International Dates

Changes

Title unchanged

Using International Dates

Category unchanged

How-tos

Yii version unchanged

Tags unchanged

i18n, date, validation rules

Content changed

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
[...]
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 true;
 
    }
 
    else
 
        
return false;
}
[...]
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, 
 
                        'yyyy-MM-dd'
 
                    ),
 
                    'short',null
 
                );
 
        }
 
        elseif ($column->dbType == 'datetime' || $column->dbType == 'timestamp')
 
        {
 
            
$this->$columnName = Yii::app()->dateFormatter->formatDateTime(                     CDateTimeParser::parse($this->$columnName, 'yyyy-MM-dd hh:mm:ss'),'short','short');
 
}
 
}
 
return true

 
                        $this->$columnName, 
 
                        'yyyy-MM-dd hh:mm:ss'
 
                    ),
 
                    'short','short'
 
                );
 
        }
 
    }
 
    return parent::afterFind()
;
}
[...]
```php
Yii::app()->setLanguage($lang);
```

 
15 0
24 followers
Viewed: 49 294 times
Version: 1.1
Category: How-tos
Written by: Russell England
Last updated by: nkd
Created on: Apr 28, 2011
Last updated: 11 years ago
Update Article

Revisions

View all history