MYSQL Round the datetime to 15 minute

前端 未结 5 904
囚心锁ツ
囚心锁ツ 2021-01-17 12:58

I want to update the datetime round to 15 minutes in a MYSQL database table.

For Example:

If the dateTime is 2013-10-08 10:36:00, I want to conv

5条回答
  •  心在旅途
    2021-01-17 13:49

    using unix_timestamp allows quite simpe arithmetic, change 15 in the code below to some other number if needed (such as 30)

    select from_unixtime(round(unix_timestamp('2013-10-08 10:36:00')/(60*15))*(60*15)); 
    
    = October, 08 2013 10:30:00+0000
    
    select from_unixtime(round(unix_timestamp('2013-10-08 10:22:00')/(60*15))*(60*15)); 
    
    = October, 08 2013 10:15:00+0000
    

    see: http://forums.mysql.com/read.php?20,131939,201089#msg-201089

提交回复
热议问题