Hi all
I'm saving bookings to a table with a propertyID column, startDate and endDate.
Once a user has completed a form, and I've validated the requested from and to dates, and checked the to date is before the from date, I need to check the requested dates do not overlap any existing bookings for that property ID.
Is there an elegant way to do this? I've started with:
$startdate = YYYY-MM-DD (from a POST)
$enddate = YYYY-MM-DD (from a POST)
$query = "SELECT startDate AS bookedStart
, endDate AS bookedEnd
FROM bookings
WHERE propertyID = '1'";
$result = mysql_query($query)or die('Error getting bookings');
while(list($bookedStart, $bookedEnd) = mysql_fetch_array($result)){
if ($startdate <= $bookedStart && $enddate <= $bookedEnd){ // overlaps the start
}
if ($startdate >= $bookedStart && $enddate <= $bookedEnd)} // overlaps the end
}
}
Is there a better way of doing this? Perhaps by asking MySQL for a result based on the overlap requirements, or a neater way in PHP?
Thanks