Last active
August 29, 2015 14:06
-
-
Save desnudopenguino/73bb3574d388c8ec511f to your computer and use it in GitHub Desktop.
Calculates the overlapping sums of a range of numbers (used for dates in a project)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/php | |
<?php | |
function test($array) { | |
$start = 0; | |
$end = 0; | |
$total = 0; | |
foreach( $array as $index => $time) { | |
if($start == 0 AND $end == 0) { | |
$start = $time['start']; | |
$end = $time['end']; | |
} | |
elseif($time['start'] > $start AND $time['start'] < $end AND $time['end'] > $end) { | |
$end = $time['end']; | |
} | |
elseif($time['start'] > $end) { | |
$total += ($end - $start); | |
$start = $time['start']; | |
$end = $time['end']; | |
} | |
if($index == count($array) - 1) { | |
$total += ($end - $start); | |
} | |
} | |
return $total; | |
} | |
$times = array(); | |
array_push($times,array('start' => 1, 'end' => 5)); | |
array_push($times,array('start' => 3, 'end' => 8)); | |
array_push($times,array('start' => 6, 'end' => 7)); | |
array_push($times,array('start' => 12, 'end' => 14)); | |
print test($times); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment