如何从PHP数组中排除任意时间段(1-2小时)

如何从PHP数组中排除任意时间段(1-2小时),php,Php,有一系列的工作时间,如何从中排除午餐时间 $start = strtotime('8:00'); $end = strtotime('18:00'); $step = 1800; // 30 min for($i = $start; $i <= $end; $i += $step) { $arTime[] = date('H:i', $i); } print_r($arTime); $start=strottime('8:00'); $end=strottime('18:00

有一系列的工作时间,如何从中排除午餐时间

$start = strtotime('8:00');
$end = strtotime('18:00');
$step = 1800; // 30 min

for($i = $start; $i <= $end; $i += $step)
{
    $arTime[] = date('H:i', $i);
}

print_r($arTime);
$start=strottime('8:00');
$end=strottime('18:00');
$step=1800;//30分钟

对于($i=$start;$i一个允许轻松创建/操作/比较日期和时间的类,您可以使用来创建所需的数组

使用
$午餐开始
$午餐停止
设置要从结果数组中排除的时间间隔

$start = new DateTime('8:00');
$end = new DateTime('18:00');
$lunchStart = new DateTime('13:00');
$lunchStop = new DateTime('14:00');

while ($start <= $end) {
    $slot = $start->format('H:i');
    if ($start <= $lunchStart || $start >= $lunchStop) {
        $arTime[] = $slot;
    }
    $start->add(new DateInterval('PT30M')); // 30 min interval
}
$start=newdatetime('8:00');
$end=新日期时间('18:00');
$午餐开始=新日期时间('13:00');
$午餐站=新日期时间('14:00');
while($start格式('H:i');
如果($start=$stop){
$arTime[]=$slot;
}
$start->add(新日期间隔('PT30M');//30分钟间隔
}

工作

向我们展示您的尝试和您的最佳尝试($i=$start;$i