LargeTuna LargeTuna - 2 months ago 10
MySQL Question

PHP & MySQL query date time stamp between

I am attempting to query for a record. The date is stored as a date time stamp and my query looks like this:

SELECT count(c.id) as totalOrders
FROM Cart c
WHERE c.artist_id = 1
AND c.paid = 1
AND date_format(c.created, 'Y-m-d') between '2016-09-06' AND '2016-09-07'


My date time stamp is this: 2016-09-07 21:04:46

For some reason this does not return any records, why?

Answer

Change the following line:

AND date_format(c.created, 'Y-m-d') between '2016-09-06' AND '2016-09-07'

to

AND date(created) between '2016-09-06' AND '2016-09-07'

date() function will return the date from datetime and it will check in the given range.

Comments