3v4l.org

run code in 300+ PHP versions simultaneously
<?php /* This is the result i want from $uptime, all periods where there are no incidents between period_start and period_end. -------------------------------------------------- Array ( [0] => Array ( [start] => 2016-01-01 00:00:00 [end] => 2016-01-05 00:00:00 ) [1] => Array ( [start] => 2016-01-15 22:00:00 [end] => 2016-01-20 00:00:00 ) [2] => Array ( [start] => 2016-01-25 23:59:59 [end] => 2016-01-31 23:59:59 ) ) -------------------------------------------------- */ $period_start = '2016-01-01 00:00:00'; $period_end = '2016-01-31 23:59:59'; $uptime = []; $incidents = [ ['start' => '2016-01-05 00:00:00', 'end' => '2016-01-10 23:59:59'], ['start' => '2016-01-07 00:00:00', 'end' => '2016-01-15 22:00:00'], // overlapping ['start' => '2016-01-08 00:00:00', 'end' => '2016-01-12 23:59:59'], // overlapping ['start' => '2016-01-20 00:00:00', 'end' => '2016-01-25 23:59:59'], ['start' => '2016-01-23 00:00:00', 'end' => '2016-01-24 23:59:59'] // overlapping ]; // First period if(strtotime($period_start) <= strtotime($incidents[0]['start'])) { array_push($uptime, ['start' => $period_start, 'end' => $incidents[0]['start']]); } $last_end = 0; // Used for finding the last ending incident. for($i = 0;$i < sizeof($incidents);$i++) { $push = true; $last_end_j = $incidents[$i]['end']; for($j = $i+1;$j < sizeof($incidents);$j++) { if(strtotime($incidents[$i]['end']) < strtotime($incidents[$j]['start'])) { if($push) { array_push($uptime, ['start' => $last_end_j, 'end' => $incidents[$j]['start']]); } break; } else { if(strtotime($incidents[$j]['end']) > strtotime($last_end_j)) { $last_end_j = $incidents[$j]['end']; } $push = false; } } if(strtotime($incidents[$i]['end']) > strtotime($last_end)) { $last_end_j = $incidents[$i]['end']; } } // Last period if(strtotime($period_end) >= strtotime($last_end)) { array_push($uptime, ['start' => $last_end, 'end' => $period_end]); } print_r( $uptime );
Output for 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.27, 8.2.0 - 8.2.18, 8.3.0 - 8.3.4, 8.3.6
Array ( [0] => Array ( [start] => 2016-01-01 00:00:00 [end] => 2016-01-05 00:00:00 ) [1] => Array ( [start] => 2016-01-12 23:59:59 [end] => 2016-01-20 00:00:00 ) [2] => Array ( [start] => 0 [end] => 2016-01-31 23:59:59 ) )
Output for 8.3.5
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0 Array ( [0] => Array ( [start] => 2016-01-01 00:00:00 [end] => 2016-01-05 00:00:00 ) [1] => Array ( [start] => 2016-01-12 23:59:59 [end] => 2016-01-20 00:00:00 ) [2] => Array ( [start] => 0 [end] => 2016-01-31 23:59:59 ) )

preferences:
222.09 ms | 402 KiB | 290 Q