【问题标题】:Excluding specific half hour from the list of half hours for specific date从特定日期的半小时列表中排除特定半小时
【发布时间】:2014-03-20 17:32:32
【问题描述】:

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

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

我能够完美地显示每天的可用时间,除非会议已被预订为 3 月 28 日的时间 02:00PM - 02-30PM,那么这个时间将被隐藏所有天,除了只有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>

我将非常感谢任何帮助。

【问题讨论】:

  • 澄清一下,如果预定了一天的会议,是不是整天都被隐藏了?而你不想这样?
  • @JohnConde 是的,你是对的
  • 我还需要查看与此相关的其他代码。
  • @JohnConde 请查看更新后的代码,希望这会有所帮助,我也在使用 twitter bootstrap 手风琴

标签: php arrays date datetime for-loop


【解决方案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

【讨论】:

  • 您是否将开始日期设为今天? $start = new DateTime('09:00:00');
  • 在这段代码中,是的。当然,您可以使用 setDate() 轻松更改。
  • 知道为什么这不起作用吗? $start_date-&gt;setDate(2014,03,21); $start_date-&gt;format('Y-m-d'); $start_date = new DateTime('09:00:00');
  • 最后一行覆盖了前两行。试试:$start_date-&gt;setDate(2014,03,21); $start_date-&gt;setTime(9, 0); echo $start_date-&gt;format('Y-m-d H:i:s');
  • 你是个天才,稍加调整就奏效了 :) 非常感谢你......真是太感谢了
猜你喜欢
  • 1970-01-01
  • 2019-04-12
  • 2012-06-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多