Codeigniter And MYSQL DateTime Codeigniter And MYSQL DateTime codeigniter codeigniter

Codeigniter And MYSQL DateTime


I would try

$this->db->where('Status', 0);$this->db->where('Driver_ID', null);$this->db->where('DATE(Date)', date('Y-m-d'), FALSE);$query = $this->db->get('Bookings');


$params = array('Status' => 0, 'Booking_Date'=> date('Y-m-d'));$this->db->query('SELECT * FROM Bookings WHERE Status=? AND Driver_ID Is Null AND    Date(Booking_Date) = ?',$params);

Think that will work


In your code, you are saying you want a DateTime field to be NOW(). The problem is that NOW() gives a DateTime value, that is, a date in the form "YYYY-MM-DD" followed by a time in the form "HH:MM:SS".

What your query is doing is saying "Give me records where the Date is today, at this exact second". What you want is "Give me records where the Date is today".

This is why using DateTime fields in a database is usually cumbersome. You will have to convert your Date field to be just the date, without the time, using the MySQL function DATE(), and instead of NOW() which returns a DateTime value, you will want to use CURDATE() which returns only the Date value. I am not experienced with CodeIgniter specifically, but try:

$query = $this->db->get_where('Bookings', array('Status' => 0, 'Driver_ID' => null, 'DATE(Date)'=> 'CURDATE()'));

(I don't know if you can apply MySQL functions to fields with $this->db->get_where).