<?php
$input = array(
"2014-01-20 20:00:00",
"2014-01-21 20:00:00",
"2014-01-22 14:00:00",
"2014-01-22 20:00:00",
"2014-01-23 14:00:00",
"2014-01-23 20:00:00",
"2014-01-25 20:00:00",
"2014-01-26 12:00:00",
"2014-01-26 20:00:00",
"2014-01-31 20:00:00",
"2014-02-01 20:00:00"
);
$timestamps = array_map("strtotime", $input);
$calendar = array();
foreach ($timestamps as $value) {
$format = date("d.m.H", $value);
list($day, $month, $hour) = explode(".", $format);
if (!array_key_exists($month, $calendar))
$calendar[ $month ] = array();
if (!array_key_exists($day, $calendar[ $month ]))
$calendar[ $month ][ $day ] = array();
$calendar[ $month ][ $day ][] = $hour;
}
$ranges = array();
// use reference so removing objects will affect all loops
foreach ($calendar as $month => &$dates) {
foreach ($dates as $date => $hours) {
foreach ($hours as $key=>$hour) {
// start checking for groups
$currentDate = $date+1;
$range = array($date);
while (isset($dates[$currentDate]) && ($newkey = array_search($hour, $dates[$currentDate])) !== FALSE) {
$range[] = $currentDate;
unset($dates[$currentDate][$newkey]);
$currentDate++;
}
// group was found
if (count($range) != 1) {
$ranges[] = array('dates' => $range, 'hour' => $hour);
unset($dates[$date][$key]);
}
}
}
}
print_r($calendar);
print_r($ranges);
preferences:
46.13 ms | 402 KiB | 5 Q