判断时段是否重合

 

判断俩个时段是否存在交集


a1 - a2 和 b1 - b2


1、9:00 - 10:00 和 10:00 - 11:00

2、1:00 - 10:00 和 23:00 - 2:00

3、23:00 - 2:00 和 1:00 - 10:00

4、23:00 - 2:00 和 22:00 - 1:00

<?php
date_default_timezone_set('PRC');
$a1 = strtotime('23:00');
$a2 = strtotime('10:00');
$b2 = strtotime('22:00');
$b1 = strtotime('09:00');

function check($a1, $a2, $b1, $b2){
  if($a1 == $a2 || $b1 == $b2){
    return false;
  }
  if($a1 > $a2 && $b1 < $b2)  {
    $a2+=86400;
    if( ($b1<=$a2 && $b2>=$a1) || ( ($b1+86400) <= $a2 && ($b2+86400) >= $a1) ){
               return  true;
      } else {
        return false;
    }
  }
  if($a1 < $a2 && $b1 > $b2) {
    $b2+=86400;
    if( ($b1<=$a2 && $b2>=$a1) || ( $b1 <= ($a2+86400) && $b2 >= ($a1+86400) ) ){
                return  true;
        } else {
                return false;
        }
  }
  if($a1 > $a2 && $b1 > $b2) {
        $a2+=86400;
        $b2+=86400;
  }

  if($b1<=$a2 && $b2>=$a1){
       return  true;
  }
  return false;
}

$res = check($a1, $a2, $b1, $b2);
var_dump($res);

 

posted @ 2018-11-21 19:53  文沐  阅读(209)  评论(0编辑  收藏  举报