14

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.

Community
  • 1
  • 1
Michael Irigoyen
  • 22,513
  • 17
  • 89
  • 131

4 Answers4

10

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?

AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765
7

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.

Community
  • 1
  • 1
Jason McCreary
  • 71,546
  • 23
  • 135
  • 174
  • I'm beginning to think you're right. I'm now looking more closely at the timestamps, and while the times show correctly, the UNIX timestamp is incorrect, thus making it look like the shifts aren't overlapping, while they really are. – Michael Irigoyen Nov 14 '12 at 22:09
  • How are you doing the timestamp conversion? `strtotime()`? – Jason McCreary Nov 14 '12 at 22:15
  • The one item comes in a UNIX timestamp already (from the calendar framework). The other item comes from a datetime object, and then `strtotime`'d. There is an inconsistency going on somewhere there. I'm sure I'll be able to track it down. – Michael Irigoyen Nov 14 '12 at 22:23
1

You need to check if the you have a "RED" task which starts OR ends between the start and the end of an "ORANGE" task. Like this you should detect every "ORANGE" task overlapping a "RED" task.

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

EDIT: as stated by AndreyT this is kind of overkill and you can do better with less check

koopajah
  • 23,792
  • 9
  • 78
  • 104
0
if ((($red['start'] <= $orange['end']) && ($red['start'] >= $orange['start']))
    || (($red['end'] <= $orange['end']) && ($red['end'] >= $orange['start']))
    || (($red['start'] >= $orange['start']) && ($red['end'] >= $orange['end']))
   ) {
 // conflict happens if Red starts sometime between Orange start and end
 //               or if Red ends sometime between Orange start and end
 //               or if Red starts before Orange starts and ends after Orange ends

}
Chirag Kalani
  • 368
  • 2
  • 9
Isaac
  • 2,701
  • 4
  • 30
  • 47