<?php
/**
* 时间段重合判断
* @param array $data 日期数组
* @param string $fieldStart 开始日期字段名
* @param string $fieldEnd 结束日期字段名
* @return bool true为重合,false为不重合
*/
function is_time_cross(array $data, string $fieldStart = 'start_day', string $fieldEnd = 'end_day')
{
// 按开始日期排序
array_multisort(
array_column($data, $fieldStart),
SORT_ASC,
$data
);
// 冒泡判断是否满足时间段重合的条件
$num = count($data);
for ($i = 1; $i < $num; $i++) {
$pre = $data[$i-1];
$current = $data[$i];
if (strtotime($pre[$fieldStart]) <= strtotime($current[$fieldEnd]) && strtotime($current[$fieldStart]) <= strtotime($pre[$fieldEnd])) {
return true;
}
}
return false;
}
// 测试例子
$data = [
['start_day' => '2019-01-01', 'end_day' => '2019-01-31'],
['start_day' => '2018-01-01', 'end_day' => '2018-01-31'],
['start_day' => '2019-03-01', 'end_day' => '2019-05-31'],
['start_day' => '2019-10-01', 'end_day' => '2019-10-31'],
['start_day' => '2019-04-01', 'end_day' => '2019-04-10'],
];
var_dump(is_time_cross($data));
转自https://segmentfault.com/a/1190000020487996 侵删