Yii2计算gridview中2个日期之间的日期

时间:2017-12-30 02:00:09

标签: yii2 yii2-basic-app

            'class' => 'kartik\grid\FormulaColumn', 
            'header' => 'Days Work', 
            'value' => function ($model, $key, $index, $widget) { 
                $time = new \DateTime('now');
                $today = $time->format('Y-m-d');
                $p = compact('model', 'key', 'index');
                $datetime2 = $model->activedate;
                $interval = $today->diff($datetime2)->days;
                return $interval;
            },
            'headerOptions' => ['class' => 'kartik-sheet-style'],
            'hAlign' => 'right', 
            'width' => '7%',
        ],

我使用上面的代码来计算今天的日期和活动日期之间的日期。代码显示错误Call to a member function diff() on string

请告诉我我哪里错了。

谢谢。

1 个答案:

答案 0 :(得分:1)

你需要使用DateTime对象来计算diff而不是$today变量,这是一个字符串(因此你得到的错误信息)

替换:

$interval = $today->diff($datetime2)->days;

$interval = $time->diff($datetime2)->days;