PHP 中的分钟四舍五入到最近的刻钟
PHP 中的时间四舍五入到最近的刻钟是一项常见任务。本文探讨了使用 Floor() 函数的解决方案,解决了特定的查询:
$time = '10:50:00'; // Example time in datetime format $rounded_time = roundMinuteDownToNearestQuarter($time);
解决方案
要将一分钟四舍五入到最接近的一刻钟,我们需要:
代码如下:
function roundMinuteDownToNearestQuarter($time) { // Convert the time string to a timestamp $timestamp = strtotime($time); // Divide by 15 minutes (900 seconds) and round down $rounded_timestamp = floor($timestamp / 900) * 900; // Convert the rounded timestamp back to a time string return date('H:i', $rounded_timestamp); }
示例
$time = '10:50:00'; $rounded_time = roundMinuteDownToNearestQuarter($time); echo "Original: " . $time . "\n"; echo "Rounded down: " . $rounded_time . "\n";
输出:
Original: 10:50:00 Rounded down: 10:45:00
以上是如何在 PHP 中将分钟四舍五入到最近的刻钟?的详细内容。更多信息请关注PHP中文网其他相关文章!