-1

I'm looking for a way to compare two lapses of time.

If the second lapse or part of it is in the first one -> return false. Although, if the first one or a part of it is in the second one -> return false too.

I feel like I didn't have to skive math lessons...

I got

// first lapse
$a = strtotime('2016/05/04 22:50');
$b = strtotime('2016/05/20 22:15');
// second lapse
$y = strtotime('2016-05-12 12:00');
$z = strtotime('2016-05-20 10:00');

if (($y >= $a && $y <= $b) || ($z >= $a && $z <= $b)
|| ($a >= $y && $a <= $z) || ($b >= $y && $b <= $z)) 
    return false;

But it's quiet confuse in my brain. I'm not sure it does what it supposes to do. Thank you.

Edit

I found my question was duplicated with this one

Community
  • 1
  • 1
JazZ
  • 4,469
  • 2
  • 20
  • 40

1 Answers1

3

if $a <= $b and $y <= $z, count

min($b, $z) - max($a, $y)

if < 0 - no overlap    
   = 0 - common boundary point   
   > 0 - overlap
splash58
  • 26,043
  • 3
  • 22
  • 34
  • if > 0 not overlay and if > 0 overlay ? It looks pretty serious but I don't understand it well. May you add some explanations, please ? – JazZ Apr 29 '16 at 20:18
  • 2
    Thanks. typo. Simply draw on paper and you'll understand - the maximum of left edges of the segments is right than the minimum of right – splash58 Apr 29 '16 at 20:23
  • @AdrienLeber do you understand the solution ? – splash58 Apr 29 '16 at 20:38
  • Yes, I tested this solution. It seems to work great. Thank you for your help and thank you not telling me "blabla... computers are not magic... blabla... go back to school... blabla..." – JazZ Apr 29 '16 at 20:48
  • Good luck! Glad to help – splash58 Apr 29 '16 at 20:53