How to subtract hours from a datetime in MySQL?

走远了吗. 提交于 2019-11-26 06:44:19

问题


I get a datetime field, that\'s currently in the query as:

SELECT DATE_FORMAT(x.date_entered, \'%Y-%m-%d\') AS date FROM x ORDER BY date ASC

What I want to do is to subtract 3 hours from that date (GMT issues), but I can\'t do it in PHP as PHP only knows the date part, not the time.


回答1:


mySQL has DATE_SUB():

SELECT DATE_SUB(column, INTERVAL 3 HOUR)....

but would it not be better to try and sort out the underlying time zone issue instead?




回答2:


Assuming you have some timezone issue and know source and destination timezone, you could convert it like so

SELECT DATE_FORMAT(CONVERT_TZ(x.date_entered, 'UTC', 'Europe/Berlin'),
                   '%Y-%m-%d') AS date
FROM x ORDER BY date ASC;


来源:https://stackoverflow.com/questions/6020162/how-to-subtract-hours-from-a-datetime-in-mysql

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!