<?php
$from_time=explode(',', '02:02,10:35,10:25,10:30,13:00,13:05,14:00,10:00');
$to_time=explode(',', '09:20,11:05,10:55,11:00,14:00,14:05,15:00,16:00');
array_multisort($from_time);
array_multisort($to_time);
$i = 0;
$j = 0;
while(true){
$j ++;
if(!isset($to_time[$i]) || !isset($to_time[$j]))break;
if($i == $j)continue;
if($to_time[$i] >= $from_time[$j] && $to_time[$i] <= $to_time[$j]){
$from_time[$j] = $from_time[$i];
unset($to_time[$i]);
unset($from_time[$i]);
$to_time = array_values($to_time);
$from_time = array_values($from_time);
$i = 0;
$j = 0;
}
if($j >= count($to_time) - 1){
$j = 0;
$i ++;
}
}
print_r($from_time);
print_r($to_time);
/*
Array
(
[0] => 02:02
[1] => 10:00
)
Array
(
[0] => 09:20
[1] => 16:00
)
*/
//支持隔天时段
//支持隔天时段
//支持隔天时段
$from_time=explode(',', '02:02,10:35');
$to_time=explode(',', '09:20,01:45');
$from_time = array_map(function($v){
return strtotime(date('Y-m-d '.$v.':00'));
}, $from_time);
$to_time = array_map(function($v){
return strtotime(date('Y-m-d '.$v.':59'));
}, $to_time);
foreach($from_time as $k => $v){
if($v > $to_time[$k]){
$to_time[$k] += 86400;
}
}
array_multisort($from_time);
array_multisort($to_time);
$i = 0;
$j = 0;
while(true){
$j ++;
if(!isset($to_time[$i]) || !isset($to_time[$j]))break;
if($i == $j)continue;
if($to_time[$i] >= $from_time[$j] && $to_time[$i] <= $to_time[$j]){
$from_time[$j] = $from_time[$i];
unset($to_time[$i]);
unset($from_time[$i]);
$to_time = array_values($to_time);
$from_time = array_values($from_time);
$i = 0;
$j = 0;
}
if($j >= count($to_time) - 1){
$j = 0;
$i ++;
}
}
$from_time = array_map(function($v){
return date('H:i', $v);
}, $from_time);
$to_time = array_map(function($v){
return date('H:i', $v);
}, $to_time);
print_r($from_time);
print_r($to_time);
/*
Array
(
[0] => 02:02
[1] => 10:00
)
Array
(
[0] => 09:20
[1] => 16:00
)
*/
时间段交叉重新整理
于 2018-08-23 13:17:58 首次发布