php-如何使用DateTime包含时隙中断

php-如何使用DateTime包含时隙中断,php,datetime,dateinterval,timeslots,Php,Datetime,Dateinterval,Timeslots,我想创建包含开始时间、结束时间以及中断开始时间和结束时间的时段。 我已经实现了生成时间段,但是没有11:10到11:25之间的中断 输入变量 $duration = 35; // how much the is the duration of a time slot $cleanup = 0; // don't mind this $start = '09:00'; // start time $end = '12:00'; // end time $break_star

我想创建包含开始时间、结束时间以及中断开始时间和结束时间的时段。
我已经实现了生成时间段,但是没有11:10到11:25之间的中断

输入变量

$duration = 35; // how much the is the duration of a time slot
$cleanup    = 0; // don't mind this
$start    = '09:00'; // start time
$end      = '12:00'; // end time
$break_start = '11:10'; // break start
$break_end   = '11:25'; // break end

function availableSlots($duration, $cleanup, $start, $end) {
    $start         = new DateTime($start);
    $end           = new DateTime($end);
    $interval      = new DateInterval("PT" . $duration . "M");
    $cleanupInterval = new DateInterval("PT" . $cleanup . "M");
    $periods = array();

    for ($intStart = $start; $intStart < $end; $intStart->add($interval)->add($cleanupInterval)) {
        $endPeriod = clone $intStart;
        $endPeriod->add($interval);

        if ($endPeriod > $end) {
            break;
        }

        $periods[] = $intStart->format('H:i A') . ' - ' . $endPeriod->format('H:i A');
    }


    return $periods;
}
我需要实现的是从上午11:10到上午11:25的休息时间被排除在结果之外

$break_start = '11:10';
$break_end   = '11:25';
试试这个。。。 我已经修改了你的函数

function availableSlots($duration, $cleanup, $start, $end, $break_start, $break_end) {
    $start         = new DateTime($start);
    $end           = new DateTime($end);
    $break_start           = new DateTime($break_start);
    $break_end           = new DateTime($break_end);
    $interval      = new DateInterval("PT" . $duration . "M");
    $cleanupInterval = new DateInterval("PT" . $cleanup . "M");
    $periods = array();

    for ($intStart = $start; $intStart < $end; $intStart->add($interval)->add($cleanupInterval)) {
        $endPeriod = clone $intStart;
        $endPeriod->add($interval);

        if(strtotime($break_start->format('H:i A')) < strtotime($endPeriod->format('H:i A')) && strtotime($endPeriod->format('H:i A')) < strtotime($break_end->format('H:i A'))){
            $endPeriod = $break_start;
            $periods[] = $intStart->format('H:i A') . ' - ' . $endPeriod->format('H:i A');
            $intStart = $break_end;
            $endPeriod = $break_end;
            $intStart->sub($interval);
        }else{
            $periods[] = $intStart->format('H:i A') . ' - ' . $endPeriod->format('H:i A');
        }
    }


    return $periods;
}

$duration = 35; // how much the is the duration of a time slot
$cleanup    = 0; // don't mind this
$start    = '09:00'; // start time
$end      = '12:00'; // end time
$break_start = '11:10'; // break start
$break_end   = '11:25'; // break end
availableSlots($duration, $cleanup, $start, $end, $break_start, $break_end);
试试这个。。。 我已经修改了你的函数

function availableSlots($duration, $cleanup, $start, $end, $break_start, $break_end) {
    $start         = new DateTime($start);
    $end           = new DateTime($end);
    $break_start           = new DateTime($break_start);
    $break_end           = new DateTime($break_end);
    $interval      = new DateInterval("PT" . $duration . "M");
    $cleanupInterval = new DateInterval("PT" . $cleanup . "M");
    $periods = array();

    for ($intStart = $start; $intStart < $end; $intStart->add($interval)->add($cleanupInterval)) {
        $endPeriod = clone $intStart;
        $endPeriod->add($interval);

        if(strtotime($break_start->format('H:i A')) < strtotime($endPeriod->format('H:i A')) && strtotime($endPeriod->format('H:i A')) < strtotime($break_end->format('H:i A'))){
            $endPeriod = $break_start;
            $periods[] = $intStart->format('H:i A') . ' - ' . $endPeriod->format('H:i A');
            $intStart = $break_end;
            $endPeriod = $break_end;
            $intStart->sub($interval);
        }else{
            $periods[] = $intStart->format('H:i A') . ' - ' . $endPeriod->format('H:i A');
        }
    }


    return $periods;
}

$duration = 35; // how much the is the duration of a time slot
$cleanup    = 0; // don't mind this
$start    = '09:00'; // start time
$end      = '12:00'; // end time
$break_start = '11:10'; // break start
$break_end   = '11:25'; // break end
availableSlots($duration, $cleanup, $start, $end, $break_start, $break_end);

“函数将生成此输出”-您是否还可以包含精确的输入数据?输入数据为:$duration=35$开始='09:00';//上午09:00$end='12:00';//12:00 AM@batman231请编辑问题并将其包含在那里。“函数将生成此输出”-您还可以包含精确的输入数据吗?输入数据为:$duration=35$开始='09:00';//上午09:00$end='12:00';//12:00 AM@batman231编辑问题并将其包含在那里。$cleanup的目的是什么?$cleanup的目的是什么?
Array
(
    [0] => 09:00 AM - 09:35 AM
    [1] => 09:35 AM - 10:10 AM
    [2] => 10:10 AM - 10:45 AM
    [3] => 10:45 AM - 11:10 AM
    [4] => 11:25 AM - 12:00 PM
)