PHP时隙动态列表问题


PHP Timeslot dynamic listing issue

我对动态构建时隙逻辑有点困惑,你能帮助我吗?我需要显示一些时间槽列表,但每个时间槽将是两个小时。请参阅下面的列表

00:00 02:00
02 04
04 06
06 08
08 10
10 12
12 14
14 16
16 18
18 20
20 22
22 00

代码
public function getTimeSlot(){
    $custom = array();
    $M      = 'AM';
    for($i=0;$i<=23;$i++):
        $time1 = strtotime($i.':00:00');
        $time2 = strtotime(($i*2).':00:00');
        $diff = $time2 - $time1;
        $custom[] = date('H:i:s ', $diff);
    endfor; 
    pr($custom);
    exit();
}

你的问题似乎很模糊。是这样的吗?

00:00 02:00 
02:00 04:00 
04:00 06:00 
06:00 08:00 
08:00 10:00 
10:00 12:00 
12:00 14:00 
14:00 16:00 
16:00 18:00 
18:00 20:00 
20:00 22:00 
22:00 00:00

如果我只想打印一个简单的时间,我会这样做:

$start = 0; $end = 22;
$time_slot = range($start, $end, 2);
$time_slot = array_map(function($time) use ($end){
    $next = ($time != $end) ? $time + 2 : 00;
    $times = array(sprintf("%02s:00", $time), sprintf("%02s:00", $next));
    return $times;
}, $time_slot);
foreach($time_slot as $time) {
    echo "$time[0] $time[1] <br/>";
}