Jump to content

[SOLVED] Select fields between 2 dates


johnadamson

Recommended Posts

Hi,

 

I am trying to develop a booking system in PHP with MySQL but I am having trouble writing some code that checks to see if a new booking clashes with another. e.g. in the database I have a booking that runs from 2008-03-10 to 2008-03-20. When I add a booking I want to check to see if a booking already exists between these dates. Is this possible?

 

I am not an expert with MySQL but the closest I got is to carry out 2 query's:

 

SELECT propertyid, arrival_date, departure_date FROM bookings WHERE arrival_date BETWEEN '$arrival_date' AND '$departure_date'

and

SELECT propertyid, arrival_date, departure_date FROM bookings WHERE departure_date BETWEEN '$arrival_date' AND '$departure_date

 

but this obviously will not check if a booking is made in between these dates, e.g. if I try and add a booking from 2008-03-13 to 2008-03-18 it will not think that there is a collision of booking.

 

I hope I have made sence and not confused anyone.

 

Your help would be greatly appreciated.

 

Thanks.

 

John

Link to comment
Share on other sites

sorry, I'm a bit out of it today... you can simply combine both of those where clause with an OR.

 

WHERE ( arrival_date BETWEEN '$arrival_date' AND '$departure_date' ) OR ( departure_date BETWEEN '$arrival_date' AND '$departure_date )

Link to comment
Share on other sites

sorry, meant to say that it thinks there is NO collision of bookings! surely there must be a query that checks to see if the new arrival and departure date is not in the database??

 

I also tried doing:

 

SELECT propertyid, arrival_date, departure_date FROM bookings WHERE ( arrival_date BETWEEN '$arrival_date' AND '$departure_date' ) OR ( departure_date BETWEEN '$arrival_date' AND '$departure_date' ) AND (arrival_date >= '$arrival_date' AND departure_date <= '$departure_date')

 

but that didnt work either.

 

HELP

Link to comment
Share on other sites

  • 2 weeks later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.