• 0
  • 0

PHP 判断时间段是否重叠

2020-07-06 22 0 admin 所属分类:PHP 记录

传入时间区间数组

// $data = ['08:30-10:30','08:40-9:30','08:50-11:30','09:40-12:50','08:40-09:30'];
// $data = ['08:30-09:30','09:40-09:50','10:20-10:30','10:40-12:50','07:40-08:29'];
// $data = ['22:00-02:00','03:00-09:00'];
$data = ['22:00-02:00','03:00-21:00'];
function isCross($data) {
	$len = count($data);
	// 正向匹配
	for ($i=0; $i < $len-1; $i++) { 
		$one = explode("-",$data[$i]);
		for ($k=$i+1; $k <$len ; $k++) { 
			$two = explode("-",$data[$k]);
			if (($one[0]>$two[0] && $one[0]<$two[1]) || ($one[1]>$two[0] && $one[0]<$two[1])) {
				return true;
			}
		}
	}

	// 逆向匹配
	$data = array_reverse($data);
	for ($i=0; $i < $len-1; $i++) { 
		$one = explode("-",$data[$i]);
		for ($k=$i+1; $k <$len ; $k++) { 
			$two = explode("-",$data[$k]);
			if (($one[0]>$two[0] && $one[0]<$two[1]) || ($one[1]>$two[0] && $one[0]<$two[1])) {
				return true;
			}
		}
	}

	return false;
}
if (isCross($data)) {
	exit('有交叉重叠');
} else {
	exit('无交叉重叠');
}


返回顶部