CakePHP Search between 2 Date Records

后端 未结 6 1896
再見小時候
再見小時候 2020-12-11 10:20

I am building a small Web App that lets users reserve Office Rooms and Equipment. For the Reservation they enter a Start and an End Date.

When a user tries to find o

相关标签:
6条回答
  • 2020-12-11 10:37

    You cannot use database columns with this BETWEEN-syntax. If you assign strings in the array, CakePHP will quote them. Same for numeric values depending on your database setup.

    CakePHP will quote the numeric values depending on the field type in your DB.
    – see http://book.cakephp.org/2.0/en/models/retrieving-your-data.html#complex-find-conditions

    If you still want to use the BETWEEN, you can write your queries into the array key because CakePHP will not escape the keys, but only the values.

    CakePHP only escapes the array values. You should never put user data into the keys. Doing so will make you vulnerable to SQL injections.
    – see http://book.cakephp.org/2.0/en/models/retrieving-your-data.html#complex-find-conditions

    Concerning your problem:

    $this->Model->find('all', array(
        'conditions' => array(
            '"YYYY-MM-DD" BETWEEN Model.date_start AND Model.date_end',
        ),
    ));
    

    You can even work with MySQL date and time functions:

    $this->Model->find('all', array(
        'conditions' => array(
            'CURDATE() BETWEEN Model.date_start AND Model.date_end',
        ),
    ));
    

    If you use date/time functions, do not quote them. If you use a specific date put it in quotes.

    0 讨论(0)
  • 2020-12-11 10:59

    There is a simpler solution to this, but thanks for the help:

    (As a condition in the find:)

    array('Equipment.date_start <= ' => $date,
          'Equipment.date_end >= ' => $date
         ),
    
    0 讨论(0)
  • 2020-12-11 11:01

    In case if you have one date and you want to use BETWEEN

    $date_start = start date;
    $date_end = date_end;
    
    $conditions = array(
        'conditions' => array(
            'date(Equipment.create) BETWEEN ? AND ?' => array($date_start, $date_end), 
        )));
    
    $this->set('equipments', $this->Equipment->find('all', $conditions));
    

    This is the case if you have From AND To

    in your case

    $date_start = start date;
    $date_end = date_end;
    
    $conditions = array(
        'conditions' => array(
            'Equipment.start_date >=' => array($date_start), 
            'Equipment.end_date <=' => array($date_end)
        ));
    
    $this->set('equipments', $this->Equipment->find('all', $conditions));
    
    0 讨论(0)
  • 2020-12-11 11:02
    $start = date('Y-m-d');
    $end = date('Y-m-d', strtotime('+1 month'));
    
    $conditions = array('Event.start <=' => $end, 'Event.end >=' => $start);
    
    $this->Event->find('all', array('conditions' => $conditions));
    
    0 讨论(0)
  • 2020-12-11 11:03

    Here is CakePHP BETWEEN query example.

    I'm defining my arrays as variables, and then using those variables in my CakePHP find function call:

    // 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));
    

    Ref from

    0 讨论(0)
  • 2020-12-11 11:03

    If you are on cakephp 2.0 this is the answer http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#TimeHelper::daysAsSql

    If you are on cakephp 1.3 you can use TimeHelper, just import it and use the same function as per example in documentation here http://book.cakephp.org/1.3/en/view/1471/Formatting

    0 讨论(0)
提交回复
热议问题