phpdatecodeigniterquery-builderquoting

SELECT rows WHERE a column is not greater than a MySQL function's return value using CodeIgniter query builder


I'm having a problem comparing dates in a database to a week from now using activerecord. I'm trying to return a list of events with their start date less than a week from now.

The event_start_date is in the format of 2011-06-30 09:00:00

$this->db->select('event_id,title,event_start_date,location');
$this->db->where('event_start_date <=',DATE_ADD(NOW(),INTERVAL 7 DAYS ));
$query = $this->db->get('sd_events');

Solution

  • Two things. First, have you tried putting your where clause in quotes like this:

    $this->db->where('event_start_date <=','DATE_ADD(NOW(),INTERVAL 7 DAYS )');
    

    Second, if necessary just skip using the where function and put the entire query in like this:

    $this->db->query('SELECT event_id,title,event_start_date,location FROM sd_events WHERE event_start_date <= DATE_ADD(NOW(),INTERVAL 7 DAYS )');