比较yii2中的两个时间戳值

时间:2016-10-07 23:14:25

标签: php yii

我有一个模型,通过检查按时注册的数据是否在今天和昨天的午夜之间,但是它返回错误

来比较数据。

这是代码:

 $hour = 12;
    $today = strtotime($hour . ':00:00'); //returns the value of todays midnight
    $yesterday = strtotime('-1 day', $today); //yesterday midnight


  $query = TodoTruckChecklist::find()
        ->leftJoin('truck', 'todo_truck_checklist.truck_id = truck.id')
        ->where('truck.truck_status = 6')
        ->andWhere('todo_truck_checklist.done_at='.$id)

        ->andWhere(strtotime('todo_truck_checklist.registered_on')< $today)
        ->andWhere(strtotime('todo_truck_checklist.registered_on')> $yesterday)
           ;

    var_dump($query->all());

reg_on的值是日期时间字段,其值以

的形式存储

D-M-Y H:M:S

返回的错误是

SQLSTATE[42000]: Syntax error or access violation:
The SQL being executed was: SELECT `todo_truck_checklist`.* FROM 
`todo_truck_checklist` LEFT JOIN `truck` ON todo_truck_checklist.truck_id = 
 truck.id WHERE (((truck.truck_status = 6) AND 
 (todo_truck_checklist.done_at=2)) AND (1)) AND ()//this has error

我哪里出错

1 个答案:

答案 0 :(得分:0)

我希望这会对你有所帮助。

  $query = TodoTruckChecklist::find()
    ->leftJoin('truck', 'todo_truck_checklist.truck_id = truck.id')
    ->where(['truck.truck_status' => 6])
    ->andWhere(['todo_truck_checklist.done_at'=>$id])
    ->andWhere(['between','todo_truck_checklist.registered_on',$yesterday, $today])
    ->all();