问题
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