如何在yii2上使用等于当前日期的动作函数进行参数?

时间:2016-04-05 17:06:13

标签: php yii2

我希望$startdate动态地等于当前日期。

这是我的代码:

public function actionNextcourses($startdate = '2016-04-02' )
{
    $nextcourses = Courses::find()->where('start_date > :startdate', [':startdate'=>$startdate])->all();
    return $this->render('nextcourses', ['nextcourses'=>$nextcourses]);
}
public function actionPrevcourses($startdate = '2016-04-02')
{
    $prevcourses = Courses::find()->where('start_date < :startdate', [':startdate'=>$startdate])->all();
    return $this->render('prevcourses', ['prevcourses'=>$prevcourses]);
}

2 个答案:

答案 0 :(得分:1)

您可以使用null默认值来解决:

public function actionNextcourses($startdate = null )
{
    if($startDate == null) $startDate = date('Y-m-d');

    $nextcourses = Courses::find()->where('start_date > :startdate', [':startdate'=>$startdate])->all();
    return $this->render('nextcourses', ['nextcourses'=>$nextcourses]);
}

答案 1 :(得分:0)

PHP不允许为函数参数设置默认动态值。