3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php date_default_timezone_set('UTC'); $dates = 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 20:00:00', '2014-01-25 20:00:00', '2014-01-26 20:00:00', '2014-01-31 20:00:00', '2014-02-01 20:00:00' ); function getDateRanges($dates) { $tsArr = array_map('strtotime', $dates); $ranges = array(); for ($i=0, $len = count($tsArr); $i < $len; $i++) { $rstart = $tsArr[$i]; $rend = $rstart; // determine the end of the range while (isset($tsArr[$i+1]) && ($tsArr[$i+1]-$tsArr[$i]) / (60*60) <= 24) { $rend = $tsArr[++$i]; } // create DateTime objects to work with dates $startDate = new DateTime('@'.$rstart); $endDate = new DateTime('@'.$rend); // get date start and last day of month $start = $startDate->format('j'); $lastDay = $startDate->format('t'); // if last day of month, append month name if ($start == $lastDay) $start = $startDate->format('jS M'); else $start = $startDate->format('jS'); // end date $end = $endDate->format('jS M ga'); $ranges[] = $start.'-'.$end; } return $ranges; } $arr = getDateRanges($dates); echo implode(', ', $arr);
Output for 5.2.0 - 5.6.28, hhvm-3.10.0 - 3.12.0, 7.0.0 - 7.1.0
20th-23rd Jan 8pm, 25th-26th Jan 8pm, 31st Jan-1st Feb 8pm
Output for 5.1.0 - 5.1.6
Fatal error: Class 'DateTime' not found in /in/cVrfe on line 34
Process exited with code 255.
Output for 5.0.0 - 5.0.5
Fatal error: Call to undefined function date_default_timezone_set() in /in/cVrfe on line 3
Process exited with code 255.
Output for 4.4.5 - 4.4.9
Fatal error: Call to undefined function: date_default_timezone_set() in /in/cVrfe on line 3
Process exited with code 255.
Output for 4.3.2 - 4.4.4
Fatal error: Call to undefined function: date_default_timezone_set() in /in/cVrfe on line 3
Process exited with code 255.
Output for 4.3.0 - 4.3.1
Fatal error: Call to undefined function: date_default_timezone_set() in /in/cVrfe on line 3