How to format date in listing page

白昼怎懂夜的黑 提交于 2020-01-05 04:14:07

问题


I am new to Yii framework and just started to work on an existing website. I have a listing page and my requirement was to add a new field 'review_date_time' and I could managed to display it in listing. Now my question is how to change the format of the date and how to show a white space if date is not there in table field.Right now it is displaying 0000-00-00 00:00:00 if no date is there.

My code for listing

 $this->widget('zii.widgets.grid.CGridView', array(
    'id' => 'series-grid',
    'dataProvider' => $model->search(),
    'filter' => $model,
    'columns' => array(
                    array('header' => 'Category', 'name' => 'category.title'),
                    'exam_year',
                    'title',
                    'review_date_time',
                    array(
                        'class' => 'CButtonColumn',
                    ),
    ),
 ));

回答1:


If it is showing 0000-00-00 00:00:00 then it means that, that value is the default value in the db table, hence you'll have to use the value property of CDataColumn:

$this->widget('zii.widgets.grid.CGridView', array(
 'id' => 'series-grid',
 'dataProvider' => $model->search(),
 'filter' => $model,
 // 'nullDisplay'=>'',
 'columns' => array(
                array('header' => 'Category', 'name' => 'category.title'),
                'exam_year',
                'title',
                // 'review_date_time',
                array(
                    'name'=>'review_date_time',
                    'value'=>'$data->review_date_time=="0000-00-00 00:00:00"?"":$data->review_date_time'
                )
                array(
                    'class' => 'CButtonColumn',
                ),
 ),
));

Or try the nullDisplay property of CGridView (if you are storing null and overriding afterFind to format null as 0000-00-00 00:00:00):

$this->widget('zii.widgets.grid.CGridView', array(
'id' => 'series-grid',
'dataProvider' => $model->search(),
'filter' => $model,
'nullDisplay'=>'',
'columns' => array(
                array('header' => 'Category', 'name' => 'category.title'),
                'exam_year',
                'title',
                'review_date_time',
                array(
                    'class' => 'CButtonColumn',
                ),
 ),
));


来源:https://stackoverflow.com/questions/10662555/how-to-format-date-in-listing-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!