date in CGridView

Hello everyone.

I have this code:


$this->widget('zii.widgets.grid.CGridView', array(

    'dataProvider'=>$dataProvider,

    'columns'=>array(

	array('name'=>'name','header'=>"Ім'я"),

	array(   

            'name'=>'dn',

            'value'=>'date("d.m.Y",strtotime($data->dn))',

	    'header'=>'Дата народження'

        ),


	array('name'=>'posada','header'=>"Посада"),

	array('name'=>'predmet','header'=>"Предмет"),

	array(    

            'class'=>'CButtonColumn',

	    'header'=>'дії',

        ),


	),


));

It works Ok, but in my database there are many fields with date ‘0000-00-00’ and in my table generated by CGridView widget this date appears as ‘01.01.1970’. What should I do to replace ‘0000-00-00’ with empty string so that ‘01.01.1970’ would not appear when date is all zeros?

Thanks.

Just check for that value…

Something like




'value'=>'$data->dn=="0000-00-00" ? "empty date" : date("d.m.Y",strtotime($data->dn))',




'value'=>'strtotime($data->dn)?date("d.m.Y",strtotime($data->dn)):""'

Thank you guys. Both your solutions work great.

You can also write a getter method, as probably you will use this standard in more places (views, for example):

So in the model:




public function getSafeDate()

{

   if ($this->dn=="0000-00-00")

       return 'empty date';

   else

       return date("d.m.Y",strtotime($data->dn));

}



And in your grid view you can use:




 'value'=>'$data->safeDate',



This allows you, for exapmle, to change the string ‘empty date’ in one place.

Thank you, zaccaria, I find your suggestion very valuable.