display hours in half hour range

前端 未结 3 1510
名媛妹妹
名媛妹妹 2020-12-12 03:51

I am working on an appointment script that takes office opening and closing hours from database and then displays the time in between the opening and closing hours to book f

3条回答
  •  佛祖请我去吃肉
    2020-12-12 04:13

    strtotime's magic can do this

    $start = '09:00:00';
    $end = '16:00:00';
    $time = strtotime($start);
    $timeStop = strtotime($end);
    
    while($time<$timeStop) {
        echo date('H:i', $time);
        $time = strtotime('+30 minutes', $time);
        echo ' - ' . date('H:i', $time) . '
    '; }

    Result:

    09:30 - 10:00
    10:00 - 10:30
    10:30 - 11:00
    11:00 - 11:30
    11:30 - 12:00
    12:00 - 12:30
    12:30 - 13:00
    13:00 - 13:30
    13:30 - 14:00
    14:00 - 14:30
    14:30 - 15:00
    15:00 - 15:30
    15:30 - 16:00
    

提交回复
热议问题