У меня есть массив дней, подобный следующему:
array(10) {
[0]=>
string(8) "01/06/18"
[1]=>
string(8) "02/06/18"
[2]=>
string(8) "03/06/18"
[3]=>
string(8) "11/06/18"
[4]=>
string(8) "12/06/18"
[5]=>
string(8) "13/06/18"
[6]=>
string(8) "14/06/18"
[7]=>
string(8) "15/06/18"
[8]=>
string(8) "16/06/18"
[9]=>
string(8) "20/06/18"
}
Я пытаюсь найти решения, чтобы из массива дней я получал периоды следующим образом:
Period 1:
01/06/18 - 03/06/18
Period 2:
11/06/18 - 16/06/18
Period 3:
20/06/18 - 20/06/18
Какой самый короткий путь!?
Это то, что я пытаюсь, но безуспешно:
Примечание. Я изменяю форматы дат в соответствии с Ymd
private function SetPeriods($days)
{
// check if input is empty
if(count($days) == 0) return array();
// output to fill
$ranges = array();
// order dates keys in order to have the first date in temporary order
ksort($days);
// get first and last day
$firstday = key($days);
end($days);
$lastday = key($days);
// get the type of first day (actually the current day where we looks)
$current_type = $days[$firstday];
// using datetime object for easy step of 1 day
$datetime = new DateTime($days[$firstday]);
$datetime->setTime(9,0,0); // avoid time problems at midnight (it's needed?)
// do the first step outside the while
$datetime->add(new DateInterval('P1D'));
// store old value of day
$oldday = $firstday;
// build the first range
$ranges[] = array($firstday,null,$current_type);
while(($day = $datetime->format('Y-m-d')) <= $lastday) {
// if there are holes, fill it with null
if(!isset($days[$day])) {
$days[$day] = null;
}
// check if type has changed (=>need new range)
if(($days[$day] !== $current_type)) {
$ranges[count($ranges)-1][1] = $oldday;
$ranges[] = array($day,null,$days[$day]);
$current_type = $days[$day];
}
// store previous day
$oldday = $day;
// next day
$datetime->add(new DateInterval('P1D'));
}
// complete the last range
$ranges[count($ranges)-1][1] = $lastday;
// remove range of holes
foreach($ranges as $k=>$range) {
if(is_null($range[2])) {
unset($ranges[$k]);
}
}
return $ranges;
}
Проблема в том, что теперь у меня только одно свидание, я не знаю, в чем проблема, не могу понять