1

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 ();          
 } 
}
4

1 回答 1

0

您可以在呈现更新表单之前进行一些锻炼。这应该可以工作,但未在您的 操作更新之前进行测试$this->render(....);

$model->date = assign your required format here;

但是如果你需要保持这种格式,你应该避免在每个地方转换日期。只需将日期以所需格式直接保存在数据库中即可。

于 2012-07-04T15:30:20.340 回答