在半小时范围内显示小时 [英] display hours in half hour range

查看:118
本文介绍了在半小时范围内显示小时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一个约会脚本,该约会脚本从数据库中获取办公室的开放和关闭时间,然后显示在开放和关闭时间之间进行会议预订的时间,每次会议只能预订半小时,所以我需要使用单选按钮显示这样的时间

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 for a meeting, each meeting can only be booked for half an hour, so i need to display timings like this using a radio button

9:00am  - 9:30am
9:30am  - 10:00am
10:00am - 10:30am
...
...
...

我能够计算出办公室开放的总小时数,并且使用for循环,我能够显示时间

I am able to calculate the total number of hours office is open, and using for loop i am able to display the timings

$office_start_time = '09:00:00';
$office_end_time = '16:00:00';

$start = explode(':', $office_start_time);
$end = explode(':', $office_end_time);
$total_hours = $end[0] - $start[0] - ($end[1] < $start[1]);
echo 'total hours: '.$total_hours;
echo '<br>';
//exit;
?>
<table class="table table-hover table-bordered">
    <tbody>
    <?php for ($i = 0; $i < $total_hours; $i++) {
        echo "<input name='time' type='radio' value='09:00|09:30'> 9:00am-9:30am<br/>";
        ?>

    <?php } ?>
    </tbody>
</table>

我的问题是上面的代码给了我类似下面的输出,而我一直在坚持如何获得正确的结果

my problem is the above code gives me an output like below and I am stuck at how to go about getting the right outcome

如果能在显示以下时间安排方面获得帮助,我将不胜感激

I will really appreciate if i can get some help on displaying the timings as below

9:00am  - 9:30am
9:30am  - 10:00am
10:00am - 10:30am
...
...
...

推荐答案

$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;
}

查看实际效果

这篇关于在半小时范围内显示小时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆