I want to see if a time I read from a db overlaps with a time provided by a user.
My database looks like this:
--------------------------------------
Two time periods P1 and P2 overlaps if, and only if, at least one of these conditions hold:
P2.from <= P1.from <= P2.to
)P1.from <= P2.from <= P1.to
)This will catch partly overlapping periods as well as periods where one completely covers the other. One of the periods must always start (or end) inside the other if they are overlapping.
So $another_meeting
would be defined by:
$another_meeting = ($from >= $from_compare && $from <= $to_compare) ||
($from_compare >= $from && $from_compare <= $to);
You may want to change the borderline cases to strict <
checks if one event can start at the exact same time as another ends.
I'd probably solve it with something like this:
function avaliable($start, $end) {
// checks if there's a meeting between start or end
$q = "SELECT * FROM admin_boardroom_booking "
. "WHERE NOT (meeting_start BETWEEN '$end' AND '$start' "
. "OR meeting_end BETWEEN '$end' AND '$start')";
$result = mysql_query($q);
// returns true on no conflicts and false elseway
return mysql_num_rows($result) === 0;
}
Emil Vikström's answer is correct, but there is a scenario needed to be considered.
Like, One of the time ranges is a subset of another time range.
So, assume that P1{start_time, end_time}
and P2{start_time, end_time}
will overlap when any of the following is true.
Just assuming that the times are sorted in ascending order. Example below:
|-----------------------------------|
| Start time | End time | Name |
|-----------------------------------|
| 10:00 | 14:00 | P1 |
|-----------------------------------|
| 12:00 | 16:00 | P2 |
|-----------------------------------|
| 08:00 | 12:00 | P3 |
|-----------------------------------|
| 07:00 | 16:00 | P4 |
|-----------------------------------|
If you consider P1 as the base time and you want to check P2, P3, P4 against it.
true
true
true
This is how you can check if any of the time is overlapping another one or not.
In case when $to is always later than $from we can use this more shorter solutions
$another_meeting = !($from > $to_compare || $from_compare > $to);
Was just doing something similar.... but just with times....
$startTime = strtotime("7:00");
$endTime = strtotime("10:30");
$chkStartTime = strtotime("10:00");
$chkEndTime = strtotime("12:10");
if($chkStartTime > $startTime && $chkEndTime < $endTime)
{
// Check time is in between start and end time
echo "1 Time is in between start and end time";
}
elseif(($chkStartTime > $startTime && $chkStartTime < $endTime) || ($chkEndTime > $startTime && $chkEndTime < $endTime))
{
// Check start or end time is in between start and end time
echo "2 ChK start or end Time is in between start and end time";
}
elseif($chkStartTime==$startTime || $chkEndTime==$endTime)
{
// Check start or end time is at the border of start and end time
echo "3 ChK start or end Time is at the border of start and end time";
}
elseif($startTime > $chkStartTime && $endTime < $chkEndTime)
{
// start and end time is in between the check start and end time.
echo "4 start and end Time is overlapping chk start and end time";
}