PHP DateTime轮到最近10分钟 [英] PHP DateTime round up to nearest 10 minutes

查看:275
本文介绍了PHP DateTime轮到最近10分钟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从mysql字段中检索日期时间,但是我需要将向上转到最近的10分钟。

I'm retrieving a datetime from a mysql field but I need to round it up to the nearest 10 minutes.

,如果datetime是2013-11-06 14:00:01,我想把时间作为6/11/2013 14:10。

For example, If the datetime is 2013-11-06 14:00:01, I'd like to return the time as 6/11/2013 14:10.

什么是最简单的方法是这样做?

What's the easiest way to do this?

$datetime = new DateTime($mysqldata);

echo $datetime->format('d/m/Y G:i');

任何建议赞赏。

谢谢。

推荐答案

1)如果需要,将秒数设置为0(通过舍入到最近的分钟)

1) Set number of seconds to 0 if necessary (by rounding up to the nearest minute)

$second = $datetime->format("s");
if($second > 0)
    $datetime->add(new DateInterval("PT".(60-$second)."S"));

2)获取分钟

$minute = $datetime->format("i");

3)转换模10

$minute = $minute % 10;

4)如有必要,计算下一个10分钟的分钟数

4) Count minutes to next 10-multiple minutes if necessary

if($minute != 0)
{
    // Count difference
    $diff = 10 - $minute;
    // Add difference
    $datetime->add(new DateInterval("PT".$diff."M"));
}

已编辑,谢谢@Ondrej Henek和@berturion

这篇关于PHP DateTime轮到最近10分钟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆