用 PHP 减 5 分钟将当前时间舍入到 5 分钟的正确方法是什么? 我们需要输出时间字符串,所有内容都以这种格式保留 2 位小数:
$today = date("dmYHi");
例子:
16:32 -> 16:25
18:54 -> 18:45
20:04 -> 19:55
非常感谢!
最佳答案
试试这个:
$date = new DateTime();
$hours = $date->format('H')
$minutes = (integer) $date->format('i');
// round down to the nearest multiple of 5
$minutes = floor($minutes / 5 ) * 5;
// if $minutes is 0 or 5 we add a trailing 0
if($minutes < 10) {
$minutes = '0' . $minutes;
}
// output
echo $hours . ':' . $minutes;
关于php 向下舍入 5 分钟时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22898639/