
Untitled
By: a guest on
May 4th, 2012 | syntax:
None | size: 0.95 KB | hits: 11 | expires: Never
Given a decimal time value, how could I round that to an interval?
function round_decimal_time($time, $interval=15){
// Split up decimal time
$hours = (int) $time;
$minutes = $time - $hours;
// Convert base 10 minutes to base 60 minutes
$b60_m = $minutes * 60;
// Round base 60 minutes to nearest interval... (15 minutes by default)
// DONT KNOW HOW TO DO THIS PART
// If greater than or equal to 60, go up an hour
if($b60_m >= 60){
$hours += 1;
$minutes = 0;
} else {
// Otherwise, convert b60 minutes back into b10
$time = $hours + ($b60_m / 60);
}
return $time;
}
Input: 5.66 (5:39 duration)
Output: 5.75
Input: 5.6 (5:36 duration)
Output: 5.50
Input: 5.05 (5:03 duration)
Output: 5.00
round($number/$X)*$X;
round(39.6/15)*15=45
round_decimal_time($time,$round=15){
return (round($time * 60 / $round) * $round) / 60;
}
$a = 5.66;
var_dump(round($a / 0.25) * 0.25);
round(7 / 5) * 5