# What's the best way to calculate the amount of overlap between 2 sets of times.

Discussion in 'OT Technology' started by Limp_Brisket, May 4, 2009.

1. ### Limp_BrisketNew Member

Joined:
Jan 2, 2006
Messages:
48,422
0
Location:
Utah
Say i have 2 sets of unix timestamps with start and end times. What's the best way to calculate the amount of overlap between the two sets?

i'm using php btw so if there's a function that already does this then that's even better.

2. ### Limp_BrisketNew Member

Joined:
Jan 2, 2006
Messages:
48,422
0
Location:
Utah
i mean i could have three conditionals, one to check if one span is completely inside the other than take that the inner one as the overlap, if the start of one is inside the other and the end of it it is outside then take the end of the first minus the start of the second as overlap and etc... seems like there should be an easier way though

Joined:
Jan 16, 2003
Messages:
10,760
3
Location:
Plantation, FL
t0 -> t1
t2 -> t3

where t1 > t2 and t1 < t3? Is that the situation?

4. ### critter783OT Supporter

Joined:
Jul 15, 2005
Messages:
1,785
0
Case 1: They overlap somewhat.
|-----|------proc1------|
|---------|-proc2-----------------|

Case 2: One is fully contained in the other.
|----|---proc1-----|
|--|------------------proc2-------|

Looks like it would be the lesser of the ending times minus the greater of the beginning times.

5. ### Limp_BrisketNew Member

Joined:
Jan 2, 2006
Messages:
48,422
0
Location:
Utah
not necessarily. that one would be one condition, but there could also be the condition that t1 isn't between t2 and t3 but t0 is... or the condition that they both are.

6. ### Limp_BrisketNew Member

Joined:
Jan 2, 2006
Messages:
48,422
0
Location:
Utah
that's an interesting way of looking at it. i think you're correct in that statement although i'd still have to have a check to make sure that they do overlap to begin with.

i think that's how i'm complicating things, i'm using conditionals to calculate all the ways they could overlap. maybe i should just check, do they overlap and if so just do what you said.

7. ### Limp_BrisketNew Member

Joined:
Jan 2, 2006
Messages:
48,422
0
Location:
Utah
actually... if they didn't overlap and i did the lesser of the end times minus the greater of the beginning times it would probably end up a negative number, and then i'd know they don't overlap.

let me play with this

8. ### critter783OT Supporter

Joined:
Jul 15, 2005
Messages:
1,785
0
That's correct.

|---|---proc1-------|
|------------------------|-----proc2-------|

9. ### Limp_BrisketNew Member

Joined:
Jan 2, 2006
Messages:
48,422
0
Location:
Utah
ok, so i have this

Code:
```\$overlap = min(\$end, \$this->end) - max(\$start, \$this->start);
if(\$overlap > 0){
\$this->shiftHours += \$overlap / 3600;
}```
i'll give it a few tests just to make sure it does what i want, but i think it should. that was a lot simpler than what i WAS doing