Java、MySQL 或 PHP - 向下舍入到下一个 10 分钟

Java, MySQL or PHP - rounding down minutes to the next 10 minutes

我想以 10 分钟为单位向下舍入当前时间。我可以在 Java、php 或 mysql(最好)上这样做,无论哪个被认为更好。例如:

08:49:50 -> 08:40:00

14:23:12 -> 14:20:00

09:32:11 -> 09:30:00

12:20:00 -> 12:20:00

我尝试从 Round date to 10 minutes interval 中解决,但 mysql 失败了。

public static Date roundDown10Min(Date date) {

    Calendar cal = Calendar.getInstance();
    cal.setTime(date);

    int minutes = cal.get(Calendar.MINUTE);
    int roundedMinutes = (minutes/10)*10;
    cal.set(Calendar.MINUTE, roundedMinutes );
    cal.set(Calendar.SECOND, 0);
    cal.set(Calendar.MILLISECOND, 0);

    return cal.getTime();

}

这是来自以下 Whosebug post 的答案:How to round a time to the nearest 15 minute segment

SELECT FROM_UNIXTIME( TRUNCATE(UNIX_TIMESTAMP(NOW()) / 900,0)*900);

Gavin Towey 致谢!

只需将 900(15 分钟)替换为 600(10 分钟)即可。