使用PHP将当前时间向最接近的15分钟进行四舍五入。

8

有没有一种简单的方法获取当前时间并将其舍入到下一个15分钟?

例如,如果现在是12:36,则显示12:45,如果是13:01,则显示13:15。

等等。


应该能够做这个变体:https://dev59.com/cWox5IYBdhLWcg3wSCfk - JensB
1
首次点击Google -> https://dev59.com/RHE95IYBdhLWcg3wDpgG - davidkonrad
向下取整和向上取整略有不同,因此这个问题是独特的。上面的链接是关于向下取整的问题。 - steampowered
1个回答

8
这里是您的答案。
$current_date = date('d-M-Y g:i:s A');
echo $current_date."<br/>";
$current_time = strtotime($current_date);

$frac = 900;
$r = $current_time % $frac;

$new_time = $current_time + ($frac-$r);
$new_date = date('d-M-Y g:i:s A', $new_time);

echo $new_date."<br/>";

LIVE DEMO


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接