PHP Timeslot动态上市问题

时间:2014-08-07 13:09:03

标签: php arrays date

我对动态构建时隙逻辑感到有些困惑,请你帮忙解决一下。 我需要显示一些时隙列表,但每个时段将是两个小时。请参阅下面的内联列表

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();
}

1 个答案:

答案 0 :(得分:1)

你的问题似乎含糊不清。是这样的吗?

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/>";
}