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

时间:2014-03-20 17:32:33

标签: php arrays date datetime for-loop

我正在预约预约系统,并且有一个表单从用户收集开始日期和结束日期以检查顾问可用性,假设您选择开始日期为3月27日,结束日期为3月29日是三天然后我显示三行,每行包含办公室的工作时间,半小时间隔预订会议,这就是它的样子。

出现的时间应该隐藏已预订的时段。

enter image description here

我能够显示每天的可用时间完全正常,但是如果已经预订了会议,那么请说时间28日下午02:00 - 02-30 PM,那么这个时间将被隐藏所有日子除外3月28日。

以下是我为此目的使用的代码

    <?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>

我真的很感激你的帮助。

1 个答案:

答案 0 :(得分:1)

<?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