<?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 23:59:59 [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 23:59:59'], // overlapping ['start' => '2016-01-12 00:00:00', 'end' => '2016-01-13 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 uptime 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++) { $uptime_start = $incidents[$i]['end']; $uptime_end = $incidents[$i]['start']; for($j = $i+1;$j < sizeof($incidents);$j++) { // this logic doesnt work as expected! if(strtotime($uptime_start) > strtotime($incidents[$j]['start']) && strtotime($uptime_start) < strtotime($incidents[$j]['end']) && strtotime($uptime_start) < strtotime($incidents[$j+1]['start'])) { $uptime_start = $incidents[$j]['end']; $uptime_end = $incidents[$j+1]['start']; array_push($uptime, ['start' => $uptime_start, 'end' => $uptime_end]); } } // Saving for last period if(strtotime($incidents[$i]['end']) > strtotime($last_end)) { $last_end = $incidents[$i]['end']; } } // Last uptime period if(strtotime($period_end) >= strtotime($last_end)) { array_push($uptime, ['start' => $last_end, 'end' => $period_end]); } print_r( $uptime );
You have javascript disabled. You will not be able to edit any code.