phpdatetimemathcomparetimelapse

php to compare two lapses of time


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


Solution

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

    min($b, $z) - max($a, $y)
    
    if < 0 - no overlap    
       = 0 - common boundary point   
       > 0 - overlap