The user inserts a date like: 06/11/2012.
To insert into the database I use:
protected function beforeSave ()
{
if($this->date <> '')
{
list($d, $m, $y) = explode('/', $this->date);
$mk=mktime(0, 0, 0, $m, $d, $y);
$this->date = date ('Y-m-d', $mk);
}
return parent::beforeSave ();
}
Question 1) Isn't there a shorter approach ?
Anyway, it works, so I have a date in string format coming from the database date type field:
$date = '2012-11-06'
Then I use date format correctly, and I get the day and the month
yii::app()->dateFormatter->format('dd', $date);
yii::app()->dateFormatter->format('MMMM', $date);
All this seems to work fine. However, on my update form, I get a date like this:
"20/12/1106"
On the form input field, the date should be converted to:
dd/mm/yyyy
when displayed.
On my model rules here's what I have:
array('date', 'type', 'type' => 'date', 'message' => '{attribute}: Invalid Date!', 'dateFormat' => 'dd/mm/yyyy'),
Where can we control this on Yii, so that on our input field of the date appears as dd/mm/yyyy ?
I've done this, but ofcourse if we later have other controller then update, we have to change this. This seems NOT a good approach. :(
protected function afterFind ()
{
if(Yii::app()->controller->action->id == 'update')
{
if($this->date <> '')
{
Yii::app()->dateFormatter->formatDatetime($this->date,'short');
}
return parent::afterFind ();
}
}