Compare two dates in Codeigniter and MySQL
I think you need qoutes around your date (i.e. '2011-06-08'). try this
function get_promo() { $today = date('Y-m-d'); $query = $this->db->query( "SELECT FROM tbl_event WHERE event_id = {$id} AND event_startdate <= '{$today}' AND event_enddate >= '{$today}'"); return $query;}
If your columns event_startdate
and event_enddate
are DATETIME
type but you are only interested in the date part you can do `DATE(event_enddate) to extract the date part
$this->db->where('date_start <=',date('Y-m-d'));$this->db->where('date_end >=',date('Y-m-d'));$query = $this->db->get('table');
I think u need to user date_format(), more information in this link http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-format.
Try this code:
$today = date('Y-m-d');$query = $this->db->query("SELECT FROM tbl_event WHERE event_id = $id AND DATE_FORMAT(event_startdate ,'%Y-%m-%d') >= DATE_FORMAT($today ,'%Y-%m-%d') AND DATE_FORMAT(event_enddate ,'%Y-%m-%d') <= DATE_FORMAT($today ,'%Y-%m-%d')");