MYSQL query / dates older than 1 week ago (all datetimes in UTC)

tzmatt7447 picture tzmatt7447 · Aug 30, 2010 · Viewed 91k times · Source

How do I query a mysql db to return all records with a datetime older than 1 week ago. Note that the datetime table stores everything in UTC, and I should be comparing it in that itself.

Just to be clear - I'm looking for a pure mysql query.

Answer

reko_t picture reko_t · Aug 30, 2010
SELECT * FROM tbl WHERE datetime < NOW() - INTERVAL 1 WEEK

If your table stores datetimes in different timezone than what NOW() returns, you can use UTC_TIMESTAMP() instead to get the timestamp in UTC.