webdesco Posted September 7, 2009 Share Posted September 7, 2009 hello All, i have a mysql table with the following columns mysql> SELECT * FROM chalet_availability c; +----+---------------------+------------+---------------------+---------------------+--------+------+------------------+ | id | created | propertyId | booked_from | booked_to | status | rate | comments | +----+---------------------+------------+---------------------+---------------------+--------+------+------------------+ | 1 | 2009-09-01 12:40:48 | 1 | 2009-09-20 12:41:06 | 2009-09-26 12:41:10 | 1 | 200 | booking confimed | | 2 | 2009-09-02 12:41:39 | 2 | 2009-09-18 12:41:59 | 2009-09-25 12:42:06 | 1 | 200 | booking confimed | +----+---------------------+------------+---------------------+---------------------+--------+------+------------------+ i now have a new booking, and I need to query the database to ensure that the requested property is not already booked for the requested dates, but I can not figure out a solution that feels right. so lets say my request is for property 1 from 2009-09-22 to 2009-09-28 I've got half way, i.e testing to see if there is a record in the database that matches the start date SELECT * FROM chalet_availability WHERE propertyId = '1' AND '2009-09-22' >= booked_from AND '2009-09-22' <= booked_to If the query returns 0 rows then the start date is available. but how do i do the end date, can it be done in the same query, or should I do another query on the end date, so if both queries return 0 rows then I know the booking can go ahead. However none of the above covers me for the eventuality that there may be an existing booking that has a start date and end date that fall in between my query dates i.e a weekend booking. Does this mean that I have to query each date of the requested booking i.e 7 night = 7 queries, 14 nights = 14 queries etc. As you can tell I am a newbie, so your advice and guidance on the best way to do this is much appreciated. i'm using php 5.28 MySQL 5.1.31-community Quote Link to comment https://forums.phpfreaks.com/topic/173452-2-column-mysql-date-range-search-in-php/ Share on other sites More sharing options...
PFMaBiSmAd Posted September 7, 2009 Share Posted September 7, 2009 After you have verified that the requested start/end dates are valid (the requested start date is < the requested end date), all you really need to do is make sure that requested range is outside of any existing booking range. The requested end date should be less than or equal to the existing booked_from (start) date OR the requested start date should be greater than or equal to the existing booked_to (end) date. $req_start = '2009-09-22'; $req_end = '2009-09-28'; SELECT * FROM chalet_availability WHERE propertyId = '1' AND ('$req_end' <= booked_from OR '$req_start' >= booked_to) Quote Link to comment https://forums.phpfreaks.com/topic/173452-2-column-mysql-date-range-search-in-php/#findComment-914326 Share on other sites More sharing options...
webdesco Posted September 16, 2009 Author Share Posted September 16, 2009 Thanks for your responce PFMaBiSmAd, sorry it's taken so long to reply, I came down with a bad case of tonsillitis the day after I posted the original topic. I tried your query, but It does not retrieve the results I expected. Just to confuse things I've changed the data mysql> SELECT * FROM chalet_availability c; +----+---------------------+-----------------+---------------------+---------------------+--------+------+----------+ | id | created | room_propertyId | booked_from | booked_to | status | rate | comments | +----+---------------------+-----------------+---------------------+---------------------+--------+------+----------+ | 1 | 2009-09-01 00:00:00 | 1 | 2009-10-03 00:00:00 | 2009-10-17 00:00:00 | 2 | 200 | booked | | 2 | 2009-09-02 12:41:39 | 1 | 2009-10-24 00:00:00 | 2009-11-07 00:00:00 | 2 | 200 | booked | | 3 | 2009-09-03 00:00:00 | 1 | 2009-11-21 00:00:00 | 2009-11-28 00:00:00 | 2 | 2 | booked | | 4 | 2009-09-13 00:00:00 | 4 | 2009-10-01 00:00:00 | 2009-11-01 00:00:00 | 4 | 400 | NULL | | 8 | 2009-06-09 00:00:00 | 1 | 2009-12-19 00:00:00 | 2010-01-02 00:00:00 | 2 | 200 | booked | +----+---------------------+-----------------+---------------------+---------------------+--------+------+----------+ 5 rows in set (0.00 sec) Using the above data if I was to check if there is an existing booking from 2009-11-14 to 2009-11-20 I should retrieve 0 rows as there are no existing booking, however using your query below I retrieve 5 rows! mysql> SELECT * FROM chalet_availability WHERE '2009-11-20' <= booked_from OR '2009-11-14' >= booked_to; +----+---------------------+-----------------+---------------------+---------------------+--------+------+----------+ | id | created | room_propertyId | booked_from | booked_to | status | rate | comments | +----+---------------------+-----------------+---------------------+---------------------+--------+------+----------+ | 1 | 2009-09-01 00:00:00 | 1 | 2009-10-03 00:00:00 | 2009-10-17 00:00:00 | 2 | 200 | booked | | 2 | 2009-09-02 12:41:39 | 1 | 2009-10-24 00:00:00 | 2009-11-07 00:00:00 | 2 | 200 | booked | | 3 | 2009-09-03 00:00:00 | 1 | 2009-11-21 00:00:00 | 2009-11-28 00:00:00 | 2 | 2 | booked | | 4 | 2009-09-13 00:00:00 | 4 | 2009-10-01 00:00:00 | 2009-11-01 00:00:00 | 4 | 400 | NULL | | 8 | 2009-06-09 00:00:00 | 1 | 2009-12-19 00:00:00 | 2010-01-02 00:00:00 | 2 | 200 | booked | +----+---------------------+-----------------+---------------------+---------------------+--------+------+----------+ 5 rows in set (0.00 sec) any ideas on why this is so? Quote Link to comment https://forums.phpfreaks.com/topic/173452-2-column-mysql-date-range-search-in-php/#findComment-919660 Share on other sites More sharing options...
PFMaBiSmAd Posted September 16, 2009 Share Posted September 16, 2009 The query checks if the requested range is available (open/free) for any property, not if the requested range is booked. Quote Link to comment https://forums.phpfreaks.com/topic/173452-2-column-mysql-date-range-search-in-php/#findComment-919669 Share on other sites More sharing options...
webdesco Posted September 17, 2009 Author Share Posted September 17, 2009 I'm trying to figure out how this can be implimented, and the only way that I can think of is to firs count the total number of rows in the chalet_availability table then do your query; if the number of rows from your query equals the total number of rows in the table then the date is available, otherwise it is not. Is this how you would do it? I've been trying to come up with a query that gives me a list of properties that are not available for the period, then i could use the resulting array to unset another array that contains all the properties. The end result being an array of properties that are available. I'm close with the following query that detects if the start or end dates overlap an existing booking, however it fails to catch a booking that is between the dates. SELECT DISTINCT room_propertyId FROM chalet_availability where (status != 1) AND (('2009-11-07' > booked_from AND '2009-11-07' < booked_to) OR ('2009-11-21' > booked_from AND '2009-11-21' < booked_to) ) Quote Link to comment https://forums.phpfreaks.com/topic/173452-2-column-mysql-date-range-search-in-php/#findComment-919957 Share on other sites More sharing options...
webdesco Posted September 17, 2009 Author Share Posted September 17, 2009 Okay, the following query does it all, but I can't help thinking that there is an easier way of doing it! SELECT DISTINCT room_propertyId FROM chalet_availability where (status != 1) AND ((('2009-11-7' > booked_from AND '2009-11-7' < booked_to) OR ('2009-11-21' > booked_from AND '2009-11-21' < booked_to)) OR ((booked_from > '2009-11-7' AND booked_from < '2009-11-21') OR (booked_to > '2009-11-7' AND booked_to < '2009-11-21'))) Results: if there is no conflict (i.e the dates are available) then zero rows are returned, if there are existing bookings, then the id of the property that is booked will be returned. comments would be appreciated from the pro's Quote Link to comment https://forums.phpfreaks.com/topic/173452-2-column-mysql-date-range-search-in-php/#findComment-919963 Share on other sites More sharing options...
webdesco Posted September 17, 2009 Author Share Posted September 17, 2009 The query checks if the requested range is available (open/free) for any property, not if the requested range is booked. I'm not sure whether I made my objective clear, I'm looking for a way to generate a list of properties that are available for a certain date period, I don't think your query can achieve this. Quote Link to comment https://forums.phpfreaks.com/topic/173452-2-column-mysql-date-range-search-in-php/#findComment-919990 Share on other sites More sharing options...
fenway Posted September 21, 2009 Share Posted September 21, 2009 Well, if you have the date range, you simply need to check in any db record overlaps. Quote Link to comment https://forums.phpfreaks.com/topic/173452-2-column-mysql-date-range-search-in-php/#findComment-922571 Share on other sites More sharing options...
webdesco Posted September 22, 2009 Author Share Posted September 22, 2009 Well, if you have the date range, you simply need to check in any db record overlaps. I'm not sure what you mean Quote Link to comment https://forums.phpfreaks.com/topic/173452-2-column-mysql-date-range-search-in-php/#findComment-922755 Share on other sites More sharing options...
fenway Posted October 2, 2009 Share Posted October 2, 2009 Well, there are 4 options: 1) both start/end are are before your date range 2) both start/end are are after your date range 3) start is before, end is during/after 4) start is during, end is during/after 1 & 2 are good... 3 & 4 are overlaps. Quote Link to comment https://forums.phpfreaks.com/topic/173452-2-column-mysql-date-range-search-in-php/#findComment-929247 Share on other sites More sharing options...
crirus Posted August 31, 2010 Share Posted August 31, 2010 Hello I am trying to do something similar, but I want to be able to select even partial booked, form your query above if a day or more within your search interval is booked you receive property as booked. Any clue how to make it available also when only partial booked? thanks Cristian Quote Link to comment https://forums.phpfreaks.com/topic/173452-2-column-mysql-date-range-search-in-php/#findComment-1105532 Share on other sites More sharing options...
fenway Posted August 31, 2010 Share Posted August 31, 2010 Wow, you resurrected a really old thread... Quote Link to comment https://forums.phpfreaks.com/topic/173452-2-column-mysql-date-range-search-in-php/#findComment-1105758 Share on other sites More sharing options...
crirus Posted September 1, 2010 Share Posted September 1, 2010 Had a reason I also figured out how. I have properties rented First I rule out properties that will never match a search based on all other search rules. On remaining ones I select all the bookings that intersect my search days. In PHP I build an array with all days booked $booked[]='yyyy-mm-dd'; I build another array with search interval Finally I check if any day in the search interval is not in the $booked array. If one is not found I break the loop and go to next property. Will see how this behave on really large database and booking table. Quote Link to comment https://forums.phpfreaks.com/topic/173452-2-column-mysql-date-range-search-in-php/#findComment-1105972 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.