Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determining if two time ranges overlap at any point [duplicate]

Tags:

algorithm

php

Possible Duplicate:
Determine Whether Two Date Ranges Overlap

I am trying to work out if two time ranges in PHP overlap. I've been referring to Determine Whether Two Date Ranges Overlap for my initial try, however, it's not matching all cases. If a time range is nested in between the start and end times of another time range, it's not being matched. If it overlaps the beginning or the end of the shift, or if the shifts are exact matches, it works as expected.

Check out this image of what I'm talking about:

enter image description here

Basically, I am trying to hide any orange shifts if they overlap any red shifts anywhere. Here's the relevant portion of code I'm trying to use to make this happen.

if(($red['start'] <= $orange['end']) && ($red['end'] >= $orange['start'])) {
    //Conflict handling
}

The values of the variables are UNIX timestamps. Working through the numbers logically, I understand why the statement above fails. There are obviously ways I could do more logic to determine if the one shift falls in the other shift (which is what I may need to do), but I was hoping for a more universal catch.

EDIT: Adding the values of each block's start and end time. I agree what I have should work. The fact that it isn't is where my issue lies. I'm probably overlooking something dumb.

orange-start = 1352899800
orange-end = 1352907000

red-start = 1352923200
red-end = 1352926200

Therefore my logic would state:

if((1352923200 <= 1352907000) && (1352926200 >= 1352899800))

So following that, the first comparison fails.

EDIT 2: It looks like my logic is sound (which I thought was the case), and my issue is something related to the UNIX timestamp not matching the actual time being displayed. I thank those who worked though this with me and help me discover that as being the issue. I wish I could accept both Andrey's and Jason's answers.

like image 919
Michael Irigoyen Avatar asked Nov 14 '12 21:11

Michael Irigoyen


2 Answers

If you have two ranges [b1, e1] and [b2, e2] (where it is already established that b1 < e1 and b2 < e2) then the overlap is detected by the following logical expression

not (e2 < b1 or e1 < b2)

which can be rewritten as

e2 >= b1 and e1 >= b2

In your syntax that would be

if(($orange['end'] >= $red['start']) && ($red['end'] >= $orange['start'])) {
   //Conflict handling
}

I.e. you got it correctly. Why you are claiming "Working through the numbers logically, I understand why the statement above fails." is not clear to me. What exactly fails? (And I don't know why is everyone coming up with ridiculously "overengineered" checks, with more than two comparisons.)

Of course, you have to decide whether touching ranges are considered overlapping and adjust the strictness of the comparisons accordingly.

P.S. The sample ranges you provided in your edit do not overlap, and your comparison correctly recognizes it as no-conflict situation. I.e. everything works as it should. Where do you see the problem?

like image 133
AnT Avatar answered Oct 06 '22 00:10

AnT


The logic is correct. The timestamps you provided for $red (8-8:50pm) and $orange (1:30-3:30pm) do not overlap.

Given correct values (that reflect your screenshot), the overlap is indeed found:

function show_date($value, $key) {
    echo $key, ': ', date('r', $value), PHP_EOL;
}

$red = array('start' => strtotime('today, 2pm'), 'end' => strtotime('today, 2:45pm'));
$orange = array('start' => strtotime('today, 1:30pm'), 'end' => strtotime('today, 4pm'));

array_walk($red, 'show_date');
array_walk($orange, 'show_date');

if (($red['start'] <= $orange['end']) && ($red['end'] >= $orange['start'])) {
    echo 'Conflict handling';
}

My guess would be you have a timezone conversion issue.

like image 22
Jason McCreary Avatar answered Oct 05 '22 23:10

Jason McCreary