php 向下舍入 5 分钟时间

标签 php time rounding

用 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/

相关文章:

php - Cakephp 如何基于另一个虚拟字段创建虚拟字段

sql - 带时区查询的时间不符合预期

c++ - C++ 中的舍入错误

mysql 小数点四舍五入

c++ - 来自 time.h 的 time() 是单调的吗?

html - "width: calc(100%/3);"工作不正常

javascript - PHP AJAX 上传无法正常使用 php ://input

linux - 超过 300 秒的最大执行时间

php - 如何使用别名和 $_GET 参数配置 yii2 urlManager 规则

JAVA:经过这么多时间后中断线程