display hours in half hour range

前端 未结 3 1506
名媛妹妹
名媛妹妹 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 " {$previous}-{$current}
    "; } $previous = $current; }

    See it in action

提交回复
热议问题