【发布时间】:2012-01-26 21:45:42
【问题描述】:
例如,给定十进制值 5.66,表示 5 小时 39 分钟,我如何将这个数字四舍五入为 5 小时 45 分钟(最接近的 15 分钟间隔),即 5.75。
同样,如果我有 5 小时 36 分钟或 5.6,这比 5:45 更接近 5:30,所以我想从中得到 5.5。
尝试用 PHP 编写。
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
【问题讨论】: