3

I Have a query as follows:

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

The Date field is a datetime type, and I want to be garbing records where their date is the same as today, however the above, and everything else I have tired does not work.

Can anyone one show me how to correctly pull records that date it today, ignoring the time part of datetime.

Thanks for any help

UPDATE

I Have now converted the query to the following

$start = date('Y-m-d 00:00:00');
    $end = date('Y-m-d 23:59:59');
    $query = $this->db->get_where('Bookings', array('Status' => 0, 'Driver_ID' => null, 'Date' => 'BETWEEN '.$start.' AND '.$end));

However still no luck, just retuning no results!

4 Answers 4

4

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');
Sign up to request clarification or add additional context in comments.

2 Comments

Confirmed, this does work, but FALSE had to be removed and qoutes need to be added around (date) like so $this->db->where('Status', 0); $this->db->where('Driver_ID', null); $this->db->where('DATE(Booking_Date)', date('Y-m-d')); $query = $this->db->get('Bookings');
I think this is a better answer, then in this case as its closer to the functions you were trying to use in the first place
2
$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

2 Comments

could you update query to have Booking_Date for referencing my column, I have got confused with the column names and function calls
Ah Got it going in the end Thanks very much!
1

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).

5 Comments

Hi thanks for the responses, but so far neither has worked, trying to fiddle with the answers but just displays nothing so far :(
Is there a way to see what SQL the get_where function is generating? What if you just ran the query SELECT * FROM Bookings WHERE Status = 0 AND Driver_ID = NULL and DATE(`Date`) = CURDATE()? Perhaps the query is failing because you have a field named "Date", when that is a keyword in MySQL (because of the DATE() function)?
Updated name from Date to Booking_Date with no luck, and also tried the manual SQL statement you suggested with no luck. Below is one of the stored dates I have 2012-04-17 13:45:24
Can you tell me what you are getting for: SELECT CURDATE(), SELECT DATE(Booking_Date) FROM Bookings, and date(Y-m-d).
Just to be clear date('Y-m-d') is a PHP function, not MySQL (also forgot the quotes around the parameters, too late to edit).
0

MySql standard datetime format is date("Y-m-d H:i:s").

So you query will need to get everything for the current day, so a where clause someything like;

SELECT * FROM `Bookings` WHERE `Date` BETWEEN date("Y-m-d 00:00:00") AND date("Y-m-d 23:59:59")

2 Comments

Hi thanks for the responses, but so far neither has worked, trying to fiddle with the answers but just displays nothing so far :(
TBH i would do something like @Logan Serman suggested. I would store all the dates as a UNIX timestamp. It makes playing with the values easy as they are just integers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.