compare mysql datetime timestamp days ago

To get all entries in a table with a “datetime” field that is newer than 3 days ago (i.e. maybe a user last logged in in the last 72 hours):

mysql> select id,timestamp_field from sometable where timestamp_field > ( CURDATE() - INTERVAL 3 DAY );

Note that you could just use “where like” if you were looking for just today, or just yesterday, or just some month. In this case “newer than 3 days” means “since right now minus 72 hours”

There’s a multitude of ways to compare timestamps from a point in time (i.e. now()).

Here’s older than 3 months:

 timestamp_field < ( CURDATE() - INTERVAL 3 MONTH )
share: