Laravel从给定的时间范围中删除时间块

时间:2016-04-14 09:13:16

标签: php laravel laravel-5.2

我正在处理Laravel 5.2

根据项目要求,我的时间范围从08:00 AM08:00 PM

即。 array ("start"=> "08:00:00", "end" => "20:00:00");

那里有几处休息,例如01:00 PM02:00 PM04:00 PM04:30 PM06:35 PM07:10 PM06:45 PM07: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"]
)

使用PHPLaravel我该如何实现?

1 个答案:

答案 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
        )

)