I want to get records between to date from mysql database. I wrote this but it does not work :
$from_date = "2012-06-19";
$to_date = "2012-06-24";
SELECT *
FROM `contracts`
WHERE `indate` >= '$from_date'
AND `indate` <= '$to_date'
ORDER BY `id` DESC
My dates are : 2012-06-20 , 2012-06-21 , 2012-06-22 , 2012-06-23
What you’re doing should work fine. I select from date fields like this all the time at work.
I used mysql variables here of course, but there’s nothing to them; they just contain strings. You could do this instead:
The O.P. are right though, BETWEEN is much more succinct:
The magic of the date comparison is because the field is of date type.