PHP free time blocks from start and end dates
我试图纯粹从答案中获得这些空闲时间块,代码工作正常,但我无法构造中断(到可用时隙的间隔时间)。
按照此答案,我在将30分钟的休息时间添加到可用插槽时遇到问题。 30分钟后可以使用下一个广告位。 上面的方法返回类似,
上午10:00-上午10:30
上午10:30-上午11:00
上午11:00-上午11:30
11:30 AM-12:00 PM
预期结果为
上午10:00-上午10:30
上午10:45-上午11:15
11:30 AM-12:00 PM
我想在每个时隙开始时添加休息时间。
$meeting = array(array('start' => '2016-11-14 16:00:00',
'end' => '2016-11-14 16:30:00'
),
array('start' => '2016-11-14 16:45:00',
'end' => '2016-11-14 20:00:00'
),
array('start' => '2016-11-14 14:00:00',
'end' => '2016-11-14 15:00:00'
),
array('start' => '2016-11-14 13:00:00',
'end' => '2016-11-14 14:00:00'
),
array('start' => '2016-11-11 15:20:00',
'end' => '2016-11-11 16:00:00'
),
array('start' => '2016-11-11 14:00:00',
'end' => '2016-11-11 15:00:00'
),
array('start' => '2016-11-07 07:00:00',
'end' => '2016-11-09 15:00:00'
)
);
$min = "09:00:00";
$max = "17:00:00";
$start = "2030-01-01 00:00:00";
$end = "2000-01-01 00:00:00";
$free = [['start' => "2000-01-01 00:00:00", 'end' => "2030-01-01 00:00:00"]];
foreach($meeting as $m){
foreach($free as $k=>$f){
if($m['start']>$f['start'] && $m['start']<=$f['end']){
$free[$k]['end'] = $m['start'];
if($m['end']<$f['end'])
$free[] = ['start'=>$m['end'], 'end'=>$f['end']];
}
elseif($m['end']<$f['end'] && $m['end']>$f['start']){
$free[$k]['start'] = $m['end'];
}
}
$start = min($start,$m['start']);
$end = max($end,$m['end']);
}
$begin = new DateTime( $start );
$end = new DateTime( $end );
$interval = DateInterval::createFromDateString('1 day');
$period = new DatePeriod($begin, $interval, $end);
$daily_free = [];
foreach ( $period as $dt ){
$m = [
'start'=>date('Y-m-d ',strtotime($dt->format('Y-m-d').' -1 day')).$max,
'end'=>$dt->format('Y-m-d ').$min
];
foreach($free as $k=>$f){
if($m['start']>$f['start'] && $m['start']<=$f['end']){
$free[$k]['end'] = $m['start'];
if($m['end']<$f['end'])
$free[] = ['start'=>$m['end'], 'end'=>$f['end']];
}
elseif($m['end']<$f['end'] && $m['end']>$f['start']){
$free[$k]['start'] = $m['end'];
}
}
}
foreach($free as $k=>$f){
$s = explode(" ",$f['start']);
$e = explode(" ",$f['end']);
if($s[0] == $e[0]){
$daily_free[$s[0]][] = ['start_free'=>$s[1], 'end_free'=>$e[1]];
}
}