I did a little test, creating a table with id, booked_from, and booked_to fields and then ran the query I sent you plugging in several different dates. All that was returned were results where not in the user selected date range:
CREATE TABLE `test` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`booked_from` DATE NOT NULL ,
`booked_to` DATE NOT NULL
)
INSERT INTO `test` ( `id` , `booked_from` , `booked_to` )
VALUES (NULL , '2006-05-03', '2006-05-07'),
(NULL , '2006-05-02', '2006-05-08'),
(NULL , '2006-05-09', '2006-05-11'),
(NULL , '2006-05-05', '2006-05-15'),
(NULL , '2006-05-11', '2006-05-22'),
(NULL , '2006-05-13', '2006-05-21'),
(NULL , '2006-05-17', '2006-05-24'),
(NULL , '2006-05-13', '2006-05-20');
Data:
Rows: 8
id booked_from booked_to
1 2006-05-03 2006-05-07
2 2006-05-02 2006-05-08
3 2006-05-09 2006-05-11
4 2006-05-05 2006-05-15
5 2006-05-11 2006-05-22
6 2006-05-13 2006-05-21
7 2006-05-17 2006-05-24
8 2006-05-13 2006-05-20
$datefrom = '2006-05-10';
$dateto = '2006-05-15';
SELECT *
FROM test
WHERE booked_from NOT BETWEEN '$datefrom' AND '$dateto'
AND booked_to NOT BETWEEN '$datefrom' AND '$dateto'
Results:
Rows: 3
id booked_from booked_to
1 2006-05-03 2006-05-07
2 2006-05-02 2006-05-08
7 2006-05-17 2006-05-24
As you can see, these are the only dates that are not within the user entered date range.
I'm curious as to how you have your tables set up. It may be that you are not providing yourself with enough information.
Each property has an id number, right? And when a property is booked, it is written to a rental table which contains at least, a unique id, the property id, the booked_from date, and the booked_to date. Am I right so far?
So the user comes to your website and wants to book property A31, for instance. The user selects the property of interest and the dates the family wants to go to the beach or whatever. You want a query to say YES the property is available during those dates, or NO it's already booked by some other lucky family. Am I right so far? If that's all you want to do, then the query above will work, except that you also have to search on the property_id number, so it would be something like:
SELECT *
FROM test
WHERE property_id = '$propertyID'
AND booked_from NOT BETWEEN '$datefrom' AND '$dateto'
AND booked_to NOT BETWEEN '$datefrom' AND '$dateto'
You have to have a unique record id and a property id in the table. Each property will hopefully be rented by many different familys, so that the property_id would be in the table many times.
Now if you're wanting to throw a calendar up that showes the user the dates that the property's rental availability, say putting an X or something on the booked dates, then that's a bit different.
I just bought a book that might help you figure out how your tables should be. It's "Beginning MySQL Database Design and Optimization, From Novice to Professional" by Jon Stephens and Chad Russel, pub. Apress. It will help you to normalize your tables and not duplicate any information.
I'll be glad to help you more, but first I have to figure out exactly what you're trying to accomplish and what your tables look like. You can PM me if you like. Then add the solution when you get it the way you want it...
Thanks,
Alisa