我如何将2个数组与日期进行比较并获得唯一周期

时间:2019-01-30 17:34:22

标签: php arrays

我有2个带有数据的数组:

1)预订日期的数组

    Array
    (
        [0] => Array
            (
                [date] => 2019-01-30
                [check_in] => 2019-01-28
                [check_out] => 2019-02-02
            )

        [1] => Array
            (
                [date] => 2019-01-31
                [check_in] => 2019-01-28
                [check_out] => 2019-02-02
            )

        [2] => Array
            (
                [date] => 2019-02-01
                [check_in] => 2019-01-28
                [check_out] => 2019-02-02
            )

        [3] => Array
            (
                [date] => 2019-02-05
                [check_in] => 2019-02-05
                [check_out] => 2019-02-06
            )

    )

2)两个日期之间的日期数组,即今天之后的10天。

Array
(

    [0] => 2019-01-30

    [1] => 2019-01-31

    [2] => 2019-02-01

    [3] => 2019-02-02

    [4] => 2019-02-03

    [5] => 2019-02-04

    [6] => 2019-02-05

    [7] => 2019-02-06

    [8] => 2019-02-07

    [9] => 2019-02-08

)

我该如何获得一个可用日期的第一期,以对此日期进行折扣,我想我需要进行递归foreach,但也许有人会给出很好的秘诀,说明如何进行更多质量的编码。 在此示例中,我需要获取:

数组     (

    [0] => 2019-02-02

    [1] => 2019-02-03

    [2] => 2019-03-04

)

UPD查找获取免费日期数组的简短方法:

            $arr = []; \\days of available with same format

            foreach ($array as $key => $item) { //$array array of busy days
                $arr[] = $item["date"];
            }
            $datesArray = $arrayAllDays; //$arrayAllDays array next 10 days 

            $arr2 = array_diff($datesArray, $arr); //when I got 2 array with same format I can compare 2 array.

Outpoot:

Array
(
    [3] => 2019-02-02
    [4] => 2019-02-03
    [5] => 2019-02-04
    [7] => 2019-02-06
    [8] => 2019-02-07
    [9] => 2019-02-08
)

因此,密钥现在是(缺少0,1,2 cos预订),3,4,5(缺少6 cos预订)等。

所以我现在需要得到 3,4,5 的值,怎么做?

2 个答案:

答案 0 :(得分:1)

这是一种方式:

// Assumes given dates are valid
function getAvailableDays($startDate, $endDate, $reservations, $dayInSeconds = 86400)
{
    // Lets create a range of integers that represent each date
    $availableTimestamps = range(
        date_create($startDate)->getTimestamp(),
        date_create($endDate)->getTimestamp(),
        $dayInSeconds
    );

    foreach ($reservations as $reservation) {
        // Similar to above, create a range for each reservation's check-in/check-out
        $reservedTimestamps = range(
            date_create($reservation['check_in'])->getTimestamp(),
            date_create($reservation['check_out'])->getTimestamp(),
            $dayInSeconds
        );

        // ..And here is where the magic happens
        // By using array_diff, we'll continuously remove reserved
        // data points from the available data points
        $availableTimestamps = array_diff($availableTimestamps, $reservedTimestamps);
    }

    // Return the result as an array of available dates in format like: 2019-01-31
    return array_map(function ($timestamp) {
        return date('Y-m-d', $timestamp);
    }, $availableTimestamps);
}

假设我们有以下输入数据:

$startDate = '2019-01-31';
$endDate   = '2019-02-09';

$reservations = [
    [
        'date'      => '2019-01-28',
        'check_in'  => '2019-01-28',
        'check_out' => '2019-02-02'
    ],

    [
        'date'      => '2019-02-05',
        'check_in'  => '2019-02-05',
        'check_out' => '2019-02-06'
    ]
];

这是输出内容:

Array
(
    [3] => 2019-02-03
    [4] => 2019-02-04
    [7] => 2019-02-07
    [8] => 2019-02-08
    [9] => 2019-02-09
)

答案 1 :(得分:0)

<?php

$next_10 = 
[
  '2019-01-30'.
  '2019-01-31',
  '2019-02-01',
  '2019-02-02',
  '2019-02-03',
  '2019-02-04',
  '2019-02-05',
  '2019-02-06',
  '2019-02-07',
  '2019-02-08',
];

$reserved = [
  [
    'date' => '2019-01-30',
    'check_in' => '2019-01-28',
    'check_out' => '2019-02-02'
  ],
  [
    'date' => '2019-01-31',
    'check_in' => '2019-01-28',
    'check_out' => '2019-02-02'
  ],
  [
    'date' => '2019-02-01',
    'check_in' => '2019-01-28',
    'check_out' => '2019-02-02'
  ],
  [
    'date' => '2019-02-05',
    'check_in' => '2019-02-05',
    'check_out' => '2019-02-06'
  ]
];

$available = array(); // will contain the available dates
foreach($next_10 as $day) // we check each of the next 10 days
{
  $free = TRUE; // we assume the date is available unless we prove it is not
  foreach($reserved as $res) // loop through each booking
  {
    if($day >= $res['check_in'] AND $day < $res['check_out'])
    {
      // this date is already booked
      $free = FALSE;
      break;
    }
  }
  if($free) $available[] = $day; // this date was really not booked
  if(count($available) == 3) break;
}

var_dump($available);

运行上面的代码时,将产生以下输出

enter image description here