Revision #2 has been created by Maurizio Domba Cerin on Apr 28, 2011, 12:01:00 PM with the memo:
typo and code formating
« previous (#1) next (#3) »
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);
}
$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');
}
}
$this->$columnName,
'yyyy-MM-dd hh:mm:ss'
),
'short','short'
);
}
}
return true;
}
```
Changing the target language[...]