计算所有房间何时繁忙

时间:2017-10-01 15:51:02

标签: php datetime

假设我们有两个可预订的房间 - Room 1 Room 2 。客房在预订时不可见,但在用户预订房间时会自动分发。我们想要计算两者同时被预订的时间。换句话说 - 什么时候没有空房。

问题是当我们添加第三个房间 - 房间3 时 - 它将不再起作用,因为它只比较两个房间。如何调整(或完全替换)逻辑,以便房间数量可变?

输入:

<?php
$busy_rooms = array (
  'room_1' => 
  array (
    0 => 
    array (
      'from' => 
      DateTime::__set_state(array(
         'date' => '2017-10-02 11:00:00.000000',
         'timezone_type' => 1,
         'timezone' => '+02:00',
      )),
      'to' => 
      DateTime::__set_state(array(
         'date' => '2017-10-02 12:00:00.000000',
         'timezone_type' => 1,
         'timezone' => '+02:00',
      )),
    ),
  ),
  'room_2' => 
  array (
    0 => 
    array (
      'from' => 
      DateTime::__set_state(array(
         'date' => '2017-10-02 09:00:00.000000',
         'timezone_type' => 1,
         'timezone' => '+02:00',
      )),
      'to' => 
      DateTime::__set_state(array(
         'date' => '2017-10-02 16:00:00.000000',
         'timezone_type' => 1,
         'timezone' => '+02:00',
      )),
    )
  )
);

在上面的示例中,特定日期的11:00-12:00之间没有空房。目前的逻辑似乎正确处理了这一点。

当前逻辑:

<?php
$busy_times = array()

foreach($busy_rooms as $a_id => $a_busy_times) {
    foreach($busy_rooms as $b_id => $b_busy_times) {

        // Never compare a room with itself
        if($b_id === $a_id) continue;

        foreach($a_busy_times as $a_busy) {
            foreach($b_busy_times as $b_busy) {

                // B within A
                //    |--B--|
                // |-----A-----|
                if($b_busy['from'] >= $a_busy['from'] && $b_busy['to'] <= $a_busy['to']) {
                    $busy_times[] = array(
                        'from' => clone $b_busy['from'],
                        'to' => clone $b_busy['to']
                    );
                }

                // B-from within A, B-to after A
                //    |--B--|
                // |--A--|
                elseif($b_busy['from'] >= $a_busy['from'] && $b_busy['to'] >= $a_busy['to'] && $b_busy['from'] <= $a_busy['to']) {
                    $busy_times[] = array(
                        'from' => clone $b_busy['from'],
                        'to' => clone $a_busy['to']
                    );
                }

                // B-from before A, B-to within A
                // |--B--|
                //    |--A--|
                elseif($b_busy['from'] <= $a_busy['from'] && $b_busy['to'] <= $a_busy['to'] && $b_busy['to'] >= $a_busy['from']) {
                    $busy_times[] = array(
                        'from' => clone $b_busy['from'],
                        'to' => clone $a_busy['to']
                    );
                }
            }
        }
    }
}

输出$busy_times的内容):

<?php
array (
  array (
    'from' => 
    DateTime::__set_state(array(
       'date' => '2017-10-02 11:00:00.000000',
       'timezone_type' => 1,
       'timezone' => '+02:00',
    )),
    'to' => 
    DateTime::__set_state(array(
       'date' => '2017-10-02 12:00:00.000000',
       'timezone_type' => 1,
       'timezone' => '+02:00',
    )),
  )
);

0 个答案:

没有答案