CBC_NS
CBC_NS

Reputation: 1957

Assistance with Search MySql DB using date and time range

I have a search page for my users and one of the things I want them to be able to do is search items in our DB using a time range.

Currently, my query looks like this:

select ****
from ****
where created_on >='{$errTimeDayOne}' and created_on <= '{$errTimeDayTwo}'

So, say $errTimeDayOne = 2013-03-24 00:00:00 and $errTimeDayTwo = 2013-04-01 01:00:00 it will search between that range. I'm trying to search between 00:00:00 - 01:00:00 through dates 2013-03-24 - 2013-04-01. Any thoughts or suggestions?

Update:

My query now looks like this:

SELECT call_id, priority_name, caller_name,
cust_name, cust_rep, caller_dept, 
DATE_FORMAT(created_on , '%Y-%m-%d') AS created_on, 
DATE_FORMAT(updated_on, '%Y-%m-%d') AS updated_on, 
source_name, created_by, cat_name, subcat_name 
FROM calls INNER JOIN categories ON calls.cat_id = 
categories.cat_id INNER JOIN ticket_priority ON 
calls.ticket_priority = ticket_priority.priority_id 
INNER JOIN ticket_source ON calls.ticket_source = 
ticket_source.source_id LEFT OUTER JOIN subcategories 
ON calls.subcat_id = subcategories.subcat_id WHERE 
created_by = 'brett.little' AND (DATE(created_on) 
between '2013-03-01' and '2013-08-01') AND 
(TIME(created_on) between '1:00:00' and '16:00:00')

It is displaying results when I leave out '(TIME(created_on) between '1:00:00' and '16:00:00')'. Might this have anything to do with the date_format in the beginning of the query?

Upvotes: 0

Views: 67

Answers (1)

Marc B
Marc B

Reputation: 360702

You'll have to enhance the clause to check the times and dates separately:

WHERE (DATE(created_on) BETWEEN '2013-03-24' and '2013-04-01')
   AND (TIME(created_on) BETWEEN '00:00:00' AND '01:00:00')

Upvotes: 1

Related Questions