3v4l.org

run code in 300+ PHP versions simultaneously
<?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.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.28, 7.0.0 - 7.0.20, 7.1.0 - 7.1.10, 7.2.29 - 7.2.33, 7.3.12 - 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
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.3.11, 4.4.0 - 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

preferences:
233.02 ms | 401 KiB | 353 Q