一个计划任务,有N个时间段,每个时间段执行不同的业务,时间段存在包含,不存在交集!
如何把业务分割出来执行而有不影响业务!在特定的时间段里,就要执行就近原则的业务!
以时间点为分割!
$list = [
['start'=>'08:00','end'=>'16:00','desc'=>'通用'],
['start'=>'09:00','end'=>'10:00','desc'=>'特殊'],
['start'=>'10:00','end'=>'11:00','desc'=>'检查'],
['start'=>'12:00','end'=>'13:00','desc'=>'午检'],
['start'=>'14:00','end'=>'15:00','desc'=>'上课'],
['start'=>'13:30','end'=>'16:00','desc'=>'wudu'],
['start'=>'17:00','end'=>'20:00','desc'=>'夜幕']
];
//对list进行排序
$lists = array();
$end_lsit = array();
foreach ($list as $key=>$val){
$lists[$val['start']] = $val;
$end_lsit[$val['end']] = $val;
}
ksort($lists);
ksort($end_lsit);
//取出所有时间点
$starts = array_column($list, 'start');
$ends = array_column($list, 'end');
$times = array_merge($starts,$ends);
$times = array_unique($times);
sort($times);
$list_data = array();
foreach ($times as $key=>$val){
if (!empty($lists[$val])){
if ($key < count($times) -1){
if ($lists[$val]['end'] <= $times[$key+1]){
$list_data[] = $lists[$val];
}elseif ($lists[$val]['end'] > $times[$key+1]){
$self_data = $lists[$val];
$self_data['end'] = $times[$key+1];
$list_data[] = $self_data;
}
}
}else{
$self_lists = array();
foreach ($lists as $keys=>$vals){
if ($vals['start'] <= $val && $vals['end'] > $val){
$self_lists[] = $vals;
}
}
$last_data = array();
foreach ($self_lists as $keyss=>$valss){
$last_data[(int)$valss['end'] - (int)$valss['start']] = $valss;
}
ksort($last_data);
$last_data = array_values($last_data);
if (!empty($last_data)){
if($key < count($times) -1){
if ($val != $times[$key+1]){
if ($val != $times[$key+1]){
$last_data[0]['start'] = $val;
$last_data[0]['end'] = $times[$key+1];
}
}
}else{
if ($val != $last_data[0]['end']){
$last_data[0]['start'] = $val;
}
}
if (!empty($last_data)){
$list_data[] = $last_data[0];
}
}
}
}
print_r($list_data);
网友评论