3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?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.6.28, hhvm-3.10.0 - 3.12.0, 7.0.0 - 7.1.0
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.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.