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

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

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!