MYSQL Date Time Round To Nearest Hour

前端 未结 8 521
孤独总比滥情好
孤独总比滥情好 2020-12-09 14:52

I have a date time field in a MySQL database and wish to output the result to the nearest hour.

e.g. 2012-04-01 00:00:01 should read 2012-04-01 00

8条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-09 15:13

    To round down to the current hour, select:

    FROM_UNIXTIME(FLOOR(UNIX_TIMESTAMP(column_name) / 3600) * 3600).

    The value is expressed in the current time zone doc

提交回复
热议问题