我需要使用PHP检查范围是否被动态添加的子范围覆盖。
例如范围:2019-06-07 10:00:00 - 2019-06-09 11:00:00
带有子范围:
2019-06-07 10:00:00 - 2019-06-07 23:59:59
2019-06-08 00:00:00 - 2019-06-09 11:00:00
可以
但具有子范围:
2019-06-07 10:00:00 - 2019-06-07 23:59:59
2019-06-08 00:00:00 - 2019-06-09 10:00:00
2019-06-09 10:30:00 - 2019-06-09 11:00:00
不能正常运行,因为存在空白:2019-06-09 10:00:00 to 2019-06-09 10:30:00
我的目标只是返回true或false。
我不确定如何开始进行此检查而不会感到痛苦。
答案 0 :(得分:1)
将它们循环查看,并检查一个范围的开始元素与上一个范围的结束元素之间的时间差。
$TimeRange = array();
$TimeRange[0]['start'] = '2019-06-07 10:00:00';
$TimeRange[0]['end'] = '2019-06-07 23:59:59';
$TimeRange[1]['start'] = '2019-06-08 00:00:00';
$TimeRange[1]['end'] = '2019-06-09 11:00:00';
for($i = 0; $i <= count($TimeRange) - 1; $i++)
{
if(isset($TimeRange[($i + 1)]))
{
$StartTime = strtotime($TimeRange[($i + 1)]['start']);
$EndTime = strtotime($TimeRange[$i]['end']);
$SecondsDiff = $StartTime - $EndTime;
echo $i . ": " . $StartTime . " vs " . $EndTime . " = " . $SecondsDiff . "<br>\n";
if($SecondsDiff > 1)
return false;
}
}
echo "<hr>Broken check<br>";
$TimeRange = array();
$TimeRange[0]['start'] = '2019-06-07 10:00:00';
$TimeRange[0]['end'] = '2019-06-07 23:59:59';
$TimeRange[1]['start'] = '2019-06-08 00:00:00';
$TimeRange[1]['end'] = '2019-06-09 10:00:00';
$TimeRange[2]['start'] = '2019-06-09 10:30:00';
$TimeRange[2]['end'] = '2019-06-09 11:00:00';
for($i = 0; $i < count($TimeRange); $i++)
{
if(isset($TimeRange[($i + 1)]))
{
$StartTime = strtotime($TimeRange[($i + 1)]['start']);
$EndTime = strtotime($TimeRange[$i]['end']);
$SecondsDiff = $StartTime - $EndTime;
echo $i . ": " . $StartTime . " vs " . $EndTime . " = " . $SecondsDiff . "<br>\n";
if($SecondsDiff > 1)
return false;
}
}