1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
<?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));
|