I have only a Note
to complexity used:
It is all about check a daterange
against daterange
, so all the stuff called EndA StartA EndB
is wired.
I would first check witch date is earlier in range. And then sort it before using it, so the Charles Bretana - maybe all cases in once?
is not needed.
Just order the dateranges
before check them in detail.
If you have done this, one single check will say if they overlap or not.
DR = DateRange , 1 = earlier start than 2, S = start , E = End
DR2S > DR1E = No Overlap (here we dont do >=
)
$ranges = array(
//only non overlap
array(array('2017-01-01','2017-01-02'),array('2017-01-03','2017-01-04')),
//rest overlapping
array(array('2017-01-01','2017-01-02'),array('2017-01-02','2017-01-04')),
array(array('2017-01-01','2017-01-02'),array('2017-01-01','2017-01-04')),
array(array('2017-01-01','2017-01-03'),array('2017-01-03','2017-01-04')),
);
foreach($ranges as $set){
//to change the order of the ranges for testing
shuffle($set);
//now order it
usort($set,function($a,$b){
if ($a[0] == $b[0]) { return 0; }
return ($a[0] < $b[0]) ? -1 : 1;
});
//show
print implode(' - ',$set[0]).' vs '.implode(' - ',$set[1]);
//test DR2S > DR1E no overlap
if($set[1][0] > $set[0][1]){
print ' NO OVERLAP<br>';
} else {
print ' OVERLAP<br>';
}
}
Results:
2017-01-01 - 2017-01-02 vs 2017-01-03 - 2017-01-04 NO OVERLAP
2017-01-01 - 2017-01-02 vs 2017-01-02 - 2017-01-04 OVERLAP
2017-01-01 - 2017-01-04 vs 2017-01-01 - 2017-01-02 OVERLAP
2017-01-01 - 2017-01-03 vs 2017-01-03 - 2017-01-04 OVERLAP
Hopefully this simplifies the topic a little bit.