Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Calculate Relative Time Ago

DZone's Guide to

Calculate Relative Time Ago

·
Free Resource

Here you may find some useful snippets for calculating relative time for different purposes: to get info from a column within certain date, like the latest 7 days, to get time interval etc.

source: http://www.apphp.com/index.php?snippet=mysql-calculate-relative-time-ago

-- Relative time with interval
DATE_SUB(NOW(), INTERVAL 25 HOUR)
 
-- Select all users that were updated in the last 24 hours.
SELECT * FROM users
WHERE users.updated > DATE_SUB(NOW(), INTERVAL 24 HOUR);
 
-- Select all users that were updated in the last 7 days.
SELECT * FROM users
WHERE users.updated > DATE_SUB(NOW(), INTERVAL 7 DAY);
Topics:

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}