For Example I have this range : 2015-06-01
to 2015-06-03
.
Now I'm going to edit this date in my form. The form however checks if the range overlaps with another range (you can't enter two leaves on the same day).
Right now I use this to verify that it does not overlap with another leave:
if($startdate !== $start_db && $enddate !== $end_db){
if($startdate <= $end_db && $enddate >= $start_db){
$error[] = 'Leave Overlaps with another leave.';
}
}
The problem occurs when I want to change the range from 2015-06-01
to 2015-06-02
, then it says it overlaps, because it does not know that its the same leave.
How can I check if the new range is within the range that's saved in the DB?
You have to check if each start and end date is between the other start and end date. So a a total of four comparisons.
if(
// Start date is in first date range
($startdate >= $start_db && $startdate <= $end_db)
||
// end date is in first date range
($enddate >= $start_db && $enddate <= $end_db)
){
$error[] = 'Leave Overlaps with another leave.';
}