<?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);
- Output for 5.0.0 - 5.0.5, 5.1.0 - 5.1.6, 5.2.0 - 5.2.17, 5.3.0 - 5.3.29, 5.4.0 - 5.4.45, 5.5.0 - 5.5.38, 5.6.0 - 5.6.40, 7.0.0 - 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.6
- Array
(
[01] => Array
(
[20] => Array
(
)
[21] => Array
(
)
[22] => Array
(
)
[23] => Array
(
)
[25] => Array
(
)
[26] => Array
(
[0] => 12
)
[31] => Array
(
[0] => 20
)
)
[02] => Array
(
[01] => Array
(
[0] => 20
)
)
)
Array
(
[0] => Array
(
[dates] => Array
(
[0] => 20
[1] => 21
[2] => 22
[3] => 23
)
[hour] => 20
)
[1] => Array
(
[dates] => Array
(
[0] => 22
[1] => 23
)
[hour] => 14
)
[2] => Array
(
[dates] => Array
(
[0] => 25
[1] => 26
)
[hour] => 20
)
)
- Output for 4.4.2 - 4.4.9
- Parse error: syntax error, unexpected '&', expecting T_VARIABLE or '$' in /in/VH43p on line 35
Process exited with code 255. - Output for 4.3.0 - 4.3.1, 4.3.5 - 4.3.11, 4.4.0 - 4.4.1
- Parse error: parse error, unexpected '&', expecting T_VARIABLE or '$' in /in/VH43p on line 35
Process exited with code 255. - Output for 4.3.2 - 4.3.4
- Parse error: parse error, expecting `T_VARIABLE' or `'$'' in /in/VH43p on line 35
Process exited with code 255.
preferences:
322.32 ms | 401 KiB | 459 Q