MySql Select All Minus X Days - Unix Timestamp

Posted 9 months ago

If you want to select all entries minus X period from a unix timestamp perform the following


 $query = "SELECT * FROM members AND join_date > UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 1 WEEK));";

A list of parameters that can be used:
MICROSECOND
SECOND
MINUTE
HOUR
DAY
WEEK
MONTH
QUARTER
YEAR
SECOND_MICROSECOND
MINUTE_MICROSECOND
MINUTE_SECOND
HOUR_MICROSECOND
HOUR_SECOND
HOUR_MINUTE
DAY_MICROSECOND
DAY_SECOND
DAY_MINUTE
DAY_HOUR
YEAR_MONTH

So you could perform the following…


 $query = "SELECT * FROM members AND join_date > UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL -1 MONTH));";


You can also use the following


WHERE date >= date_sub(now(), interval 6 week) // Using the date_sub function

WHERE date >= NOW() - INTERVAL 4 WEEK // Now as in current time

WHERE date >= DATE(NOW()) - INTERVAL 4 WEEK // or from today