I am trying to display created date column in CGridView, in DB its datatype is DateTime. I have used the below code to format the date,

array(  'name'=>'created',
                        'header'=>'created',
        'value' => 'Yii::app()->dateFormatter->format("d/M/y",strtotime($data->created))'
                       ),

I am getting date formated column in CGridView, but null values shows sysdate in column. Please see attached image,

Please provide any solution to remove sysdate in null values column

enter image description here

有帮助吗?

解决方案

Well, strtotime(null) will return false (Kami is apparently wrong).

The issue is in Yii date formater : Yii::app()->dateFormatter->format("d/M/y", false) will return current date.

You should simply create a getter in your model :

function getCreatedDate()
{
  if ($this->created===null)
    return;

  return Yii::app()->dateFormatter->format("d/M/y", $this->created);
}

And in your grid view columns, you just have to use :

  • array('name'=>'created', 'value'=>'$data->createdDate'),
  • or 'createdDate'.

其他提示

Use nullDisplay property of CGridView and set:

'nullDisplay'=>''
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top