从时间中删除秒,并将分钟四舍五入



我将时间条目存储到一个名为duration的变量中。所以现在如果我记录一个时间条目,它将是标准格式:ex(12:30:00

我想做的是删除时间条目中的秒部分,并将分钟四舍五入到每15分钟一次。此外,我想删除前面的0,如果时间在10之前。

所以09:00:00将变成9

09:30:00将到来9:30

所以12:30:00就是12:30。

12:08:00将是12:15

12:34:00将是12:30等等。

这是我使用的代码:

$duration = '';
if ($seconds < 0) {
$duration = '-';
$seconds  = abs($seconds);
}
$hours    = floor($seconds / 3600);
$seconds -= $hours * 3600;
$minutes  = floor($seconds / 60);
$seconds -= $minutes * 60;
return $duration . sprintf('%d:%02d:%02d', $hours, $minutes, $seconds);
}

这是一个样本输出:

1 => "12:20:00"

试试这个。

$currentTime = strtotime('12:34:00');
echo 'Rounded Up 15 Minutes time: ' . date('H:i',round($currentTime / (15 * 60)) * (15 * 60));
//output - Rounded Up 15 Minutes time: 12:30

如果这个

$currentTime = strtotime('12:08:00');
echo 'Rounded Up 15 Minutes time: ' . date('H:i',round($currentTime / (15 * 60)) * (15 * 60));
//output - Rounded Up 15 Minutes time: 12:15

查看演示。演示

对于圆形,您可以使用分钟零条件进行检查。

$currentTime = strtotime('9:00:00');
if(date('i',ceil($currentTime / (15 * 60)) * (15 * 60)) == 00){
echo date('H',ceil($currentTime / (15 * 60)) * (15 * 60));  
}

最新更新