【问题标题】:Yii2 Color of gridview cells, compare date conditionYii2 gridview单元格的颜色,比较日期条件
【发布时间】:2017-12-05 07:06:20
【问题描述】:
[
            'attribute' => 'duedate',
            'contentOptions' => function ($model, $key, $index, $column) {
                $time = new \DateTime('now');
                $today = $time->format('Yyyy-mm-dd');
                return ['style' => 'background-color:' 
                    . ($model->duedate < $today ? 'red' : 'white')];
            },
        ],

我使用此代码显示日期是否过期,否则将具有红色背景色和白色。

但它都显示为红色。请帮帮我。

谢谢。

【问题讨论】:

    标签: gridview yii2 yii2-basic-app


    【解决方案1】:

    您以错误的格式获取当前日期,

    只需更改您的代码即可

    [
         'attribute' => 'duedate',
         'contentOptions' => function ($model, $key, $index, $column) {
           $time = new \DateTime('now');
           $today = $time->format('Y-m-d H:i:s');
           return ['style' => 'background-color:' 
                        . (strtotime($model->duedate) < strtotime($today) ? 'red' : 'white')];
         },
    ],
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-03-01
      • 1970-01-01
      • 2017-02-28
      • 1970-01-01
      • 2013-11-29
      • 2023-03-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多