Yii2 gridview 单元格的颜色,比较日期条件

Yii2 Color of gridview cells, compare date condition

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

我使用此代码显示日期是否过期,背景颜色为红色,否则为白色。

但是都是红色的。请帮助我。

谢谢。

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

只需更改您的代码即可

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