我正在处理Laravel 5.2
根据项目要求,我的时间范围从08:00 AM
到08:00 PM
。
即。 array ("start"=> "08:00:00", "end" => "20:00:00");
那里有几处休息,例如01:00 PM
至02:00 PM
,04:00 PM
至04:30 PM
,06:35 PM
至07:10 PM
,06:45 PM
至07:15 PM
等< / p>
即。
array(
["start" => "13:00:00", "end" => "14:00:00"],
["start" => "16:00:00", "end" => "16:30:00"],
["start" => "18:35:00", "end" => "19:10:00"],
["start" => "18:45:00", "end" => "19:15:00"],
);
现在,我想从给定范围中删除所有这些时间段的中断,并希望结果在数组中,
array (
["start" => "08:00 AM", "end" => "01:00 PM"],
["start" => "01:00 PM", "end" => "04:00 PM"],
["start" => "04:30 PM", "end" => "06:35 PM"],
["start" => "07:15 PM", "end" => "08:00 PM"]
)
使用PHP
或Laravel
我该如何实现?
答案 0 :(得分:1)
function amPmToDateTime($ampm)
{
return DateTime::createFromFormat('Y-m-d h:i A', "1000-01-01 {$ampm}");
}
function timeToDateTime($time)
{
return DateTime::createFromFormat('Y-m-d H:i:s', "1000-01-01 {$time}");
}
function formatAmPm(DateTime $dateTime)
{
return $dateTime->format("h:i A");
}
function formatTime(DateTime $dateTime)
{
return $dateTime->format("H:i:s");
}
function intervalsCollide($i1, $i2) {
if( !(($i1[1] <= $i2[0] && $i1[1] < $i2[1]) || ($i1[0] > $i2[0] && $i1[0] >= $i2[1])) ) {
return true;
}
return false;
}
function intervalMinusBreak($interval, $break)
{
$intervalParts = [];
if( intervalsCollide($interval, $break) )
{
if( $interval[0] < $break[0] && $interval[1] > $break[1] )
{
$intervalParts[] = [ $interval[0], $break[0] ];
$intervalParts[] = [ $break[1], $interval[1] ];
}
else if( $interval[0] < $break[0] && $interval[1] <= $break[1] )
{
$intervalParts[] = [ $interval[0], $break[0] ];
}
else if( $interval[0] >= $break[0] && $interval[1] > $break[1] )
{
$intervalParts[] = [ $break[1], $interval[1] ];
}
}
else
{
$intervalParts[] = $interval;
}
return $intervalParts;
}
function intervalMinusBreaks($interval, $breaks)
{
$break = array_pop($breaks);
$parts = intervalMinusBreak($interval, $break);
if( count($breaks) < 1 )
{
return $parts;
}
$intervalParts = [];
foreach($parts as $part)
{
$intervalParts = array_merge($intervalParts, intervalMinusBreaks($part, $breaks));
}
return $intervalParts;
}
$interval = [ "start" => "08:00:00", "end" => "20:00:00" ];
$interval = [ timeToDateTime($interval['start']), timeToDateTime($interval['end']) ];
$breaks = [
["start" => "13:00:00", "end" => "14:00:00"],
["start" => "16:00:00", "end" => "16:30:00"],
["start" => "18:35:00", "end" => "19:10:00"],
["start" => "18:45:00", "end" => "19:15:00"],
];
$breaks = array_map(function($break) {
return [ timeToDateTime($break['start']), timeToDateTime($break['end']) ];
}, $breaks);
$timeBlocks = intervalMinusBreaks($interval, $breaks);
$timeBlocks = array_map(function($timeBlock) {
return [ 'start' => formatAmPm($timeBlock[0]), 'end' => formatAmPm($timeBlock[1]) ];
}, $timeBlocks);
echo "<pre>";
print_r($timeBlocks);
echo "</pre>";
结果:
Array
(
[0] => Array
(
[start] => 08:00 AM
[end] => 01:00 PM
)
[1] => Array
(
[start] => 02:00 PM
[end] => 04:00 PM
)
[2] => Array
(
[start] => 04:30 PM
[end] => 06:35 PM
)
[3] => Array
(
[start] => 07:15 PM
[end] => 08:00 PM
)
)