Search code examples
phpdateformattingyiilistings

How to format date in listing page


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',
                    ),
    ),
 ));

Solution

  • 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',
                    ),
     ),
    ));