Cakephp MYSQL查询:SELECT大于的日期

时间:2018-03-23 05:55:21

标签: php mysql sql datetime cakephp

我必须运行MYSQL选择查询,其中查询返回所有在今天10天后开始日期的值。我尝试过这两种方法似乎都没有用,我需要在哪里进行更改?

$fix_d_table = TableRegistry::get('fixed_departures');
$f_dates_march = $fix_d_table   ->find("all")
    ->where(['trek_id' => 3])
    ->andWhere(['month(date_from)' => 03])
    ->andWhere(['date_from' >= date("Y-m-d",strtotime("+10 days"))])
    ->order(['date_from'=>'asc'])
    ->select(['date_from', 'date_to', 'seats_available','id'])
    ->toArray();

$fix_d_table = TableRegistry::get('fixed_departures');
$f_dates_march = $fix_d_table   ->find("all")
    ->where(['trek_id' => 3])
    ->andWhere(['month(date_from)' => 03])
    ->andWhere(['date(date_from)' >= date("Y-m-d",strtotime("+10 days"))])
    ->order(['date_from'=>'asc'])
    ->select(['date_from', 'date_to', 'seats_available','id'])
    ->toArray();

2 个答案:

答案 0 :(得分:1)

尝试以下

$fix_d_table = TableRegistry::get('fixed_departures'); $f_dates_march
= $fix_d_table   ->find("all")
    ->where(['trek_id' => 3])
    ->andWhere(
        function ($exp) {
            return $exp->or_([
                'date_from <=' => date('Y-m-d', strtotime("+10 days")),
                'date_from >=' => date('Y-m-d')
            ]);
    })
    ->order(['date_from'=>'asc'])
    ->select(['date_from', 'date_to', 'seats_available','id'])
    ->toArray();

答案 1 :(得分:0)

应该是

'date_from >=' => date("Y-m-d",strtotime("+10 days"))

如果您可以依赖mysql服务器日期,则可以在查询中使用NOW()

 ->where(function($exp, $q) {
        return $exp->gte($q->func()->dateDiff([
            'date_from ' => 'identifier',
            $q->func()->now() ]),  10);
    });

这将为您提供以下SQL条件

WHERE 
(
  DATEDIFF(date_from , NOW())
)  >= 10