基本上我试图在有人试图预订超过10个连续假日的时候发出警告,并且我已经设法获得一个数组,在每组之后(当连续几天结束时)将END作为元素,但我不知道#39;知道如何拆分阵列,以便每次连续10天或更多天被预订时我可以访问日期范围。这是当前的代码:
/* == CURRENT INPUT === */
$bankhols[]="2016-05-30";
$bankhols[]="2016-08-29";
$bankhols[]="2016-12-26";
$bankhols[]="2016-12-27";
$bankhols[]="2017-01-02";
$bankhols[]="2017-04-14";
$bankhols[]="2017-04-17";
$bankhols[]="2017-05-01";
$bankhols[]="2017-05-29";
$bankhols[]="2017-08-28";
$bankhols[]="2017-12-25";
$bankhols[]="2017-12-26";
$ten_days_check = array(
'2016-07-16',
'2016-07-17',
'2016-07-18',
'2016-07-19',
'2016-07-20',
'2016-07-21',
'2016-07-22',
'2016-07-23',
'2016-07-24',
'2016-07-25',
'2016-07-26',
'2016-07-27',
'2016-07-28',
'2016-07-29',
'2016-07-30',
'2016-07-31',
'2016-08-01',
'2016-08-02',
'2016-08-03',
'2016-08-04',
'2016-08-05',
'2016-08-06',
'2016-08-07',
'2016-08-08',
'2016-08-09',
'2016-08-10',
'2016-08-11',
'2016-08-12',
//'2016-08-13',
'2016-08-14',
'2016-08-15',
'2016-08-16',
'2016-08-17',
'2016-08-18',
'2016-08-19',
'2016-08-20',
'2016-08-21',
'2016-08-22',
'2016-08-23',
'2016-08-24',
'2016-08-25',
'2016-08-26',
'2016-08-27',
'2016-08-28',
'2016-08-29',
'2016-08-30',
);
/* === ENDINPUT === */
$counter = 0;
foreach($ten_days_check as $date) {
$datetime = strtotime($date);
$nextday = date('Y-m-d', strtotime($date . ' + 1 day'));
if(date('N', $datetime) != 6 && date('N', $datetime) != 7 && !in_array($date, $bankhols)) {
$counter++;
$set_of_ten[] = $date;
}
if(!in_array($nextday, $ten_days_check)) {
if($date != $ten_days_check[count($ten_days_check) - 1]) {
//$set_of_ten[] = $counter;
$set_of_ten[] = "END";
$counter = 0;
} else {
//$set_of_ten[] = $counter;
$set_of_ten[] = "END";
}
}
}
我想举报2016-07-16和2016-07-26之间连续10天以来的事实,并继续连续10天以上的所有日期(不包括银行假期)和周末)。
答案 0 :(得分:0)
我没有完全理解你的问题,但如果你试图将日期数组分组为10个数组,那么也许你正在寻找array_chunk
。
$days_group = array_chunk($ten_days_check , 10);
这会将数组分布到一个多维数组中,每个数组包含10个元素。
旁注如果我要实现功能,那么当有人试图连续10天以上的假期时,会产生警告"在我自己的代码中,我做的如下。
$bookings
抓住并存储用户预订的日期。$bookings
数组中添加日期。array_unique
上运行$bookings
以过滤掉重复的预订。使用简单的php逻辑检查数组中是否有10个连续日期。
如果找到,则生成警告。
答案 1 :(得分:0)
<?php
function has_ten_consecutive_days($dates) {
sort($dates);
$last = null;
$consec = 0;
foreach($dates as $date) {
$ts = strtotime($date);
if($last) {
$between = $ts - $last;
if($between == 24 * 60 * 60) {
$consec++;
if($consec == 10)
return true;
} else {
$consec = 0;
}
}
$last = $ts;
}
return false;
}
创建一些日期来测试上述内容:
function timestamps_to_dateish($timestamps) {
return array_map(
function($item) {
return date('Y-m-d', $item);
},
$timestamps
);
}
// Create some sample data
$first_ts = strtotime('2016-07-27');
$timestamps = array();
// Build a list of 13 consecutive days in timestamps
foreach(range(0,12) as $num) {
$timestamps[] = $first_ts + $num * 24 * 60 * 60;
}
$ts1 = $timestamps;
$ts2 = $timestamps;
$ts2[6] += 7 * 24 * 60 * 60; // Replace the 7th day with a day a week after
$dates1 = timestamps_to_dateish($ts1);
$dates2 = timestamps_to_dateish($ts2);
var_dump($dates1);
var_dump(has_ten_consecutive_days($dates1));
var_dump($dates2);
var_dump(has_ten_consecutive_days($dates2));
输出:
array (size=13)
0 => string '2016-07-27' (length=10)
1 => string '2016-07-28' (length=10)
2 => string '2016-07-29' (length=10)
3 => string '2016-07-30' (length=10)
4 => string '2016-07-31' (length=10)
5 => string '2016-08-01' (length=10)
6 => string '2016-08-02' (length=10)
7 => string '2016-08-03' (length=10)
8 => string '2016-08-04' (length=10)
9 => string '2016-08-05' (length=10)
10 => string '2016-08-06' (length=10)
11 => string '2016-08-07' (length=10)
12 => string '2016-08-08' (length=10)
boolean true
array (size=13)
0 => string '2016-07-27' (length=10)
1 => string '2016-07-28' (length=10)
2 => string '2016-07-29' (length=10)
3 => string '2016-07-30' (length=10)
4 => string '2016-07-31' (length=10)
5 => string '2016-08-01' (length=10)
6 => string '2016-08-09' (length=10)
7 => string '2016-08-03' (length=10)
8 => string '2016-08-04' (length=10)
9 => string '2016-08-05' (length=10)
10 => string '2016-08-06' (length=10)
11 => string '2016-08-07' (length=10)
12 => string '2016-08-08' (length=10)
boolean false