Do the format conversion in the beforeSave method of your model class. Or you can write a simple behavior that implements beforeSave method, if you want to reuse the logic in multiple models.
class DateNormalizer extends CActiveRecordBehavior
{
public $dateAttributes;
public function beforeSave($event)
{
foreach ($this->dateAttributes as $attributeName) {
$currentValue = $this->owner->getAttribute($attributeName);
$normalizedValue = $this->normalize($currentValue);
$this->owner->setAttribute($attributeName, $normalizedValue);
}
}
protected function normalize($date)
{
if ('...condition...') { // check if date needs to be formatted (e.g. with a regexp)
$date = '...expression...'; // format the date as you need
}
return $date;
}
}
In the model:
public function behaviors()
{
return array(
array(
'class' => 'path.to.DateNormalizer',
'dateAttributes' => array('date_added'),
),
);
}