dongxian8048 2014-03-20 17:32
浏览 48
已采纳

从特定日期的半小时列表中排除特定的半小时

I am working on an appointment booking system, and there is a form that collects start date and end date from the user to check the consultant availability, lets suppose if you select start date as March 27 and end date as March 29 which are three days then i am displaying three rows and each row contains the operating hours of an office with half hour interval to book a meeting and this is how it looks like.

The timings that appear are supposed to hide the time slot already booked.

enter image description here

I am able to display the hours available for each day perfectly fine except that if a meeting has been booked for lets say march 28 for time 02:00PM - 02-30PM then this time is being hidden for all days except for only march 28.

Following is the code i am using for this purpose

    <?php

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

$existing_time = array(
    array(
        'start_time' => '2014-03-28T14:00:00+1100',
        'end_time' => '2014-03-28T14:30:00+1100'
    ),
    array(
        'start_time' => '2014-03-28T15:00:00+1100',
        'end_time' => '2014-03-28T15:30:00+1100'
    )
);

?>

    <div id="accordion2" class="accordion">
        <?php for ($i = 0; $i < 3; $i++) {
            ?>
            <div class="accordion-group">
                <div class="accordion-heading">
                    <a href="#collapse<?php echo $i ?>" data-parent="#accordion2" data-toggle="collapse"
                       class="accordion-toggle collapsed">
                        <?php echo date('Y-m-d', strtotime('2014-03-27' . ' + ' . $i . ' day')) ?>
                    </a>
                </div>
                <div class="accordion-body collapse" id="collapse<?php echo $i ?>" style="height: 0px;">
                    <div class="accordion-inner">

                        <?php
                        $booked = array();
                        foreach ($existing_time as $ex) {
                            $dt = new DateTime($ex['start_time']);
                            $booked[] = $dt->format('h:ia');
                        }

                        $previous = '';
                        foreach ($period as $dt) {
                            $current = $dt->format("h:ia");
                            if (!empty($previous) && !in_array($previous, $booked)) {
                                echo "<input name='time' type='radio' value='{$previous}|{$current}'> {$previous}-{$current}<br/>";
                            }
                            $previous = $current;
                        }
                        ?>
                    </div>
                </div>
            </div>
        <?php } ?>
    </div>

I will really appreciate any help on this.

  • 写回答

1条回答 默认 最新

  • dop20345 2014-03-20 18:25
    关注
    <?php
    $start    = new DateTime('09:00:00');
    $interval = new DateInterval('PT30M');
    
    $existing_time = array(
        array(
            'start_time' => '2014-03-21T14:00:00+1100',
            'end_time' => '2014-03-21T14:30:00+1100'
        ),
        array(
            'start_time' => '2014-03-20T15:00:00+1100',
            'end_time' => '2014-03-20T15:30:00+1100'
        )
    );
    
    $booked = array();
    foreach ($existing_time as $ex) {
        $dt = new DateTime($ex['start_time']);
        $booked[] = $dt->format('Y-m-d h:ia');
    }
    
    for ($i = 0; $i < 3; $i++) {
        if (0 !== $i) {
            $start->modify("+1 day");
            $start->setTime(9, 0);
        }
        $end = clone $start;
        $end = $end->setTime(16, 1); // add 1 second because last one is not included in the loop
    
        $previous = null;
        $period   = new DatePeriod($start, $interval, $end);
        foreach ($period as $dt) {
            $current  = $dt->format("h:ia");
            if (!empty($previous)) {
                $pr_short = $previous->format("h:ia");
                $pr_long  = $previous->format("Y-m-d h:ia");
                if (!in_array($pr_long, $booked)) {
                    echo "<input name='time' type='radio' value='{$pr_short}|{$current}'> {$pr_short}-{$current}<br/>";
                }
            }
            $previous = $dt;
        }
    }
    

    See it in action

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算