简体   繁体   English

从带有序列化日期的数组中查找间隔日期范围,在php中

[英]Find gap date ranges from array with serialized dates in php

For a hotel management system, I have the following php array which contains dates on which a hotel room is booked. 对于酒店管理系统,我有以下php数组 ,其中包含预订酒店房间的日期。 New guests can't be booked into the room on these dates. 这些日期不能将新客人预订到房间。

Array([0] => '2017-02-23' 
  [1] => '2017-02-24' 
  [2] => '2017-04-01' 
  [3] => '2017-04-02' 
  [4] => '2017-04-03' 
  [5] => '2017-04-04' 
  [6] => '2017-04-05' 
  [7] => '2017-04-06' 
  [8] => '2017-04-07' 
  [9] => '2017-04-08' 
  [10] => '2017-04-09' 
  [11] => '2017-04-10' 
  [12] => '2017-04-11' 
  [13] => '2017-04-12' 
  [14] => '2017-04-13' 
  [15] => '2017-04-14' 
  [16] => '2017-04-15' 
  [17] => '2017-04-16'      
  [18] => '2017-04-17' 
  [19] => '2017-04-18' 
  [20] => '2017-04-19' 
  [21] => '2017-04-20' 
  [22] => '2017-04-21' 
  [23] => '2017-04-22' 
  [24] => '2017-04-23' 
  [25] => '2017-04-24' 
  [26] => '2017-04-25' 
  [27] => '2017-04-26' 
  [28] => '2017-04-27' 
  [29] => '2017-04-28' 
  [30] => '2017-04-29' 
  [31] => '2017-04-30'
 ) 

This array tell us that the room is reserved 2 different periods: 这个数组告诉我们房间保留了2个不同的时期:

  1. 2017-02-23 to 2017-02-24 2017-02-232017-02-24
  2. 2017-04-01 to 2017-04-30 2017-04-012017-04-30

I would like to find the date ranges where the room is available, within a certain broad window. 我想在某个宽阔的窗口中找到有空房间的日期范围。

For example, if someone wanted to stay in the room from 2017-02-15 to 2017-05-07 , then I would like the system to return the following date ranges for availability: 例如,如果有人想留在房间里,从2017-02-152017-05-07 ,那么我想系统返回下列日期范围的可用性:

  1. 2017-02-15 to 2017-02-22 2017-02-152017-02-22
  2. 2017-02-25 to 2017-03-31 2017-02-252017-03-31
  3. 2017-05-01 to 2017-05-07 2017-05-012017-05-07

If someone want to stay in the room from 2017-02-22 to 2017-03-30 , then I would like the system to return the following date ranges for availability: 如果有人想在2017-02-222017-03-30日之间留在房间里,那么我希望系统返回以下日期范围以了解空房情况:

  1. 2017-02-25 to 2017-03-30 2017-02-252017-03-30

Any help? 有什么帮助吗? Thanks a lot!! 非常感谢!!

Thanks George, what a fun little challenge. 感谢乔治,这是一个有趣的小挑战。 I rolled my own date-range array-filling function which I am very proud of. 我推出了自己的日期范围数组填充函数 ,这让我感到非常自豪。 And with the help of a couple array functions and a couple foreach loops, I believe I have satisfied the brief. 借助于几个数组函数和几个foreach循环,我相信我已经满足了简短的要求。 Here is the Demo . 这是演示

Code: 码:

function fillDateRange($a,$b,$x=0,$dates=[]){
  while(end($dates)!=$b && $x=array_push($dates,date("Y-m-d",strtotime("$a +$x day"))));
  return $dates;
}

$booked=array(0=>'2017-02-03',1=>'2017-02-24',2=>'2017-04-01',3=>'2017-04-02',
              4=>'2017-04-03',5=>'2017-04-04',6=>'2017-04-05',7=>'2017-04-06',
              8=>'2017-04-07',9=>'2017-04-08',10=>'2017-04-09',11=>'2017-04-10',
              12=>'2017-04-11',13=>'2017-04-12',14=>'2017-04-13',15=>'2017-04-14',
              16=>'2017-04-15',17=>'2017-04-16',18=>'2017-04-17',19=>'2017-04-18',
              20=>'2017-04-19',21=>'2017-04-20',22=>'2017-04-21',23=>'2017-04-22',
              24=>'2017-04-23',25=>'2017-04-24',26=>'2017-04-25',27=>'2017-04-26',
              28=>'2017-04-27',29=>'2017-04-28',30=>'2017-04-29',31=>'2017-04-30');

$search=fillDateRange('2017-02-15','2017-05-07');  // pre-validated user input

// remove all dates from $search where exist in $booked...
$vacant=array_diff($search,$booked);

// group consecutive days
$date_checker=date("Y-m-d",strtotime("{$vacant[0]} -1 day"));
$x=0;
foreach($vacant as $date){
    if($date!=date("Y-m-d",strtotime("$date_checker +1 day"))){++$x;}
    $grouped[$x][]="$date";
    $date_checker=$date;
}

echo "Array of vacant date ranges:";
foreach($grouped as $group){
    $vacant_ranges[]=current($group)." to ".end($group);
}
echo "<pre>";
    var_export($vacant_ranges);
echo "</pre>";

//echo "Array of arrays containing consecutive days:";
/*foreach($grouped as $group){
    $vacant_arrays[current($group)." to ".end($group)]=$group;
}
echo "<pre>";
    var_export($vacant_arrays);
echo "</pre>";*/

Output: 输出:

Array of vacant date ranges:
array (
  0 => '2017-02-15 to 2017-02-23',
  1 => '2017-02-25 to 2017-03-31',
  2 => '2017-05-01 to 2017-05-07',
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM