根据时间段去做查询条件

来源:互联网 发布:淘宝品控违规描述不符 编辑:程序博客网 时间:2024/05/18 03:31
  public function actionSearch()
    {
       定义一个查询方法并去准备资料
        $request = \Yii::$app->request;

        $data = $request->post();//接收查询数据
        //print_r($data);die;

        unset($data['_csrf']);

     时间函数格式化

        $data['start_time'] = strtotime($data['start_time']);//转换时间格式

        $data['end_time'] = strtotime($data['end_time']);
        if($data['end_time'] < $data['start_time'])
        {
            echo '请正确输入时间';
            die();
        }
      自己尝试做where条件的拼接
        $str = ' where 1 = 1 ';
        if(!empty($data['start_time']))
        {
            $str .= ' and addtime > '.$data['start_time'];

        }


        if(!empty($data['end_time']))
        {
            $str.= ' and addtime < '.$data['end_time'];
        }
        使用框架查询
        $connection = \Yii::$app->db;
        $command = $connection->createCommand('SELECT * FROM user1'.$str);
        $arr = $command->queryAll();
        $num = count($arr);
        //var_dump($num);die;
        return $this->render('list',['arr'=>$arr,'num'=>$num]);
    }
0 0
原创粉丝点击