CakePHP查找两个日期之间的查询条件

时间:2012-08-13 09:38:29

标签: cakephp date time between

我的数据库中有一个开始日期和结束日期,表单字段中有一个$ date变量。我现在正在尝试查询所有行,其中$ date是= db中的开始/结束日期,或者是这两行之间的任何日期。

这与daysAsSql如何工作的文档中描述的内容相反。我无法弄清楚如何让它发挥作用。以下行不能用作控制器中的查找条件:

'? BETWEEN ? AND ?' => array($date, 'Item.date_start', 'Item.date_end'),

非常感谢任何帮助。这让我发疯了。

这是完整的Query和相应的SQL:

$conditions = array(
            'conditions' => array(
            'and' => array(
                '? BETWEEN ? AND ?' => array($date, 'Item.date_start', 'Item.date_end'),
                'Item.title LIKE' => "%$title%",
                'Item.status_id =' => '1'
                )));

        $this->set('items', $this->Item->find('all', $conditions));



WHERE (('2012-10-06' BETWEEN 'Item.date_start' AND 'Item.date_end') AND (`Item`.`title` LIKE '%%') AND (`Item`.`status_id` = 1))

6 个答案:

答案 0 :(得分:27)

$conditions = array(
        'conditions' => array(
        'and' => array(
                        array('Item.date_start <= ' => $date,
                              'Item.date_end >= ' => $date
                             ),
            'Item.title LIKE' => "%$title%",
            'Item.status_id =' => '1'
            )));

尝试上面的代码并询问它是否对你不起作用。

修改 根据@Aryan请求,如果我们必须找到在1个月之间注册的用户:

$start_date = '2013-05-26'; //should be in YYYY-MM-DD format
$this->User->find('all', array('conditions' => array('User.reg_date BETWEEN '.$start_date.' AND DATE_ADD('.$start_date.', INTERVAL 30 DAY)')));

答案 1 :(得分:3)

以下是CakePHP BETWEEN查询示例。

我将我的数组定义为变量,然后在我的CakePHP查找函数调用中使用这些变量:

// just return these two fields
$fields = array('uri', 'page_views');

// use this "between" range
$conditions = array('Event.date BETWEEN ? and ?' => array($start_date, $end_date));

// run the "select between" query
$results = $this->Event->find('all', 
         array('fields'=>$fields, 
               'conditions'=>$conditions));

https://msdn.microsoft.com/en-us/library/ms178594.aspx

答案 2 :(得分:0)

CakePHP 2.x查询的一般示例

        $_condition = array("TABLENAME.id" => $id, "TABLENAME.user_id" => array_unique($_array), 'date(TABLENAME.created_at) BETWEEN ? AND ?' => array($start_date, $end_date));
        $result_array = $this->TABLENAME->find("all", array(
            'fields' => array("TABLENAME.id", "TABLENAME.user_id", 'TABLENAME.created_at'),
            "conditions" => $_condition,
            "group" => array("TABLENAME.id"), //fields to GROUP BY
            'joins' => array(
                array(
                    'alias' => 'T2',
                    'table' => 'TABLENAME2',
                    'type' => 'LEFT',
                    'conditions' => array('TABLENAME.t_id = TABLENAME2.t_id')
                ),
                array(
                    'alias' => 'T3',
                    'table' => 'TABLENAME3',
                    'type' => 'LEFT',
                    'conditions' => array(
                        'IF(
                           TABLENAME.t3_id > 0,
                                T2.f_id = T3.f_id,
                                TABLENAME.ff_id = T2.ff_id
                            )'
                    )
                ),
            ),
            'recursive' => 0
                )
        );

答案 3 :(得分:0)

这是IN BETWEEN

中更有效且易于理解的cakephp 2.x查询
$testing_log_device_site_name = $testingLogData['TestingLogDevice']['Siteid'];

$conditions = array('TestingLogDevice.dateee BETWEEN ? and ?' => array($start_date, $end_date));

$results = $this->TestingLogDevice->find('all', 
    array(
         'fields'=>array('dateee','timeee','Siteid'), 
         'conditions'=>array($conditions, 'TestingLogDevice.Siteid'=>$testing_log_device_site_name)
    )
);
pr($results);

答案 4 :(得分:0)

$data=$this->post->find('all')->where([ 'id'=>$id,
'price between'=>$price1,'and'=>$price2])->toArray();

此查询的工作方式如下:

select * from post where id=$id and price between $price1 and $price2;
  

“-'price between'=> $ price1-”变为“ $ price1之间的价格”

答案 5 :(得分:-1)

使用此

       $today = new DateTime( date('Y-m-d'));

       $fiveYearsBack = $today->sub(new DateInterval('P5Y'));