How to round a time to the nearest 15 minute segment

前端 未结 3 729
谎友^
谎友^ 2020-12-09 03:00

How can I round times in MySQL to the nearest 15 minutes (e.g. 0,15,30,45)?

3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-09 03:45

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

    This can be generalized to round to any time value. 900 seconds = 15 minutes. You can replace the 900 with any other rounding factor.

提交回复
热议问题