【问题标题】:How to format date in listing page如何在列表页面中格式化日期
【发布时间】:2012-05-19 05:23:41
【问题描述】:

我是 Yii 框架的新手,刚开始在现有网站上工作。我有一个列表页面,我的要求是添加一个新字段“review_date_time”,我可以设法在列表中显示它。现在我的问题是如何更改日期的格式以及如果表格字段中没有日期如何显示空格。现在如果没有日期,它正在显示 0000-00-00 00:00:00。

我的上市代码

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

【问题讨论】:

  • 除了 nullDisplay 之外可能还有其他东西,查看修改后的答案,评论反馈。

标签: php date formatting yii listings


【解决方案1】:

如果显示 0000-00-00 00:00:00 则表示该值是 db 表中的默认值,因此您必须使用 @987654321 @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',
                ),
 ),
));

或者尝试 CGridView 的nullDisplay property(如果你存储 null 并覆盖 afterFind 以将 null 格式化为 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',
                ),
 ),
));

【讨论】:

  • 让我知道你的效果如何,如果它不起作用,还有另一种方法
  • 是的,它对我有用。在我的字段默认值为 none 之前。现在我将它更改为 null 并且这个 nullDisplay 对我有用。你也可以帮我格式化日期吗?在列数组中我尝试了这样的数组('name' => 'review_date_time', 'value' => date('m/d/Y h:i:s A', $model->review_date_time),但它给了我错误。
  • 好的,该值应该是public string $value;,因此您需要使用引号,检查我在答案中是如何做到的。现在你可以使用'value' => 'date("m/d/Y h:i:s A", $model->review_date_time)'
  • 非常感谢您的帮助,但日期格式对我不起作用。它为所有条目返回了这个 - 01/01/1970 05:30:00 AM
  • 你可以试试date format,google 看看如何使用它的例子
猜你喜欢
  • 2022-09-28
  • 2020-01-22
  • 1970-01-01
  • 2016-12-07
  • 2016-12-07
  • 1970-01-01
  • 1970-01-01
  • 2020-01-16
  • 1970-01-01
相关资源
最近更新 更多