viter
(Chekhiv)
November 22, 2010, 10:52pm
1
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.
mdomba
(Maurizio Domba Cerin)
November 22, 2010, 11:03pm
2
Just check for that value…
Something like
'value'=>'$data->dn=="0000-00-00" ? "empty date" : date("d.m.Y",strtotime($data->dn))',
rudenich
(Rudenich)
November 22, 2010, 11:13pm
3
'value'=>'strtotime($data->dn)?date("d.m.Y",strtotime($data->dn)):""'
viter
(Chekhiv)
November 23, 2010, 7:08am
4
Thank you guys. Both your solutions work great.
zaccaria
(Matteo Falsitta)
November 23, 2010, 7:16am
5
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.
viter
(Chekhiv)
November 23, 2010, 8:11am
6
Thank you, zaccaria, I find your suggestion very valuable.