<?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[$j]['end']) > strtotime($last_end_j)) {
$last_end_j = $incidents[$j]['end'];
}
if(strtotime($incidents[$i]['end']) < strtotime($incidents[$j]['start'])) {
if($push) {
array_push($uptime, ['start' => $last_end_j, 'end' => $incidents[$j]['start']]);
}
break;
}
else {
$push = false;
}
}
// Saving for last period
if(strtotime($incidents[$i]['end']) > strtotime($last_end)) {
$last_end = $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 git.master, git.master_jit, rfc.property-hooks
- Array
(
[0] => Array
(
[start] => 2016-01-01 00:00:00
[end] => 2016-01-05 00:00:00
)
[1] => Array
(
[start] => 2016-01-25 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
)
)
This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.
Active branches
Archived branches
Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page
preferences:
58.59 ms | 402 KiB | 8 Q