display hours in half hour range

前端 未结 3 1505
名媛妹妹
名媛妹妹 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:05
    $start    = new DateTime('09:00:00');
    $end      = new DateTime('16:00:01'); // add 1 second because last one is not included in the loop
    $interval = new DateInterval('PT30M');
    $period   = new DatePeriod($start, $interval, $end);
    
    $previous = '';
    foreach ($period as $dt) {
        $current = $dt->format("h:ia");
        if (!empty($previous)) {
            echo "<input name='time' type='radio' value='{$previous}|{$current}'> {$previous}-{$current}<br/>";
        }
        $previous = $current;
    }
    

    See it in action

    0 讨论(0)
  • 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) . '<br/>';
    }
    

    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
    
    0 讨论(0)
  • 2020-12-12 04:17

    How about

    <?php
        for ($i = 0; $i < $total_hours; $i++)
        {
            $time = $start[0] + $i;
            echo "<input name='time' type='radio' value='0" . $time . ":00|0" . $time . ":30'> " . $time . ":00am-" . $time . ":30am<br/>";
        }
    ?>
    
    0 讨论(0)
提交回复
热议问题