Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Subtracting a week from UNIX_TIMESTAMP

Tags:

sql

mysql

I have a query:

SELECT * FROM msc_calendar WHERE calendar_userId = 1 AND end < UNIX_TIMESTAMP() 

Is there a way to subtract a week from the timestamp, i.e. to see if end was more than a week ago?

like image 217
John Crossley Avatar asked Nov 29 '22 03:11

John Crossley


2 Answers

@EugenRieck's solution will break on edge cases like weeks where there's a daylight savings switch. It's better to use the built-in function for this, DATE_SUB:

SELECT * FROM msc_calendar WHERE calendar_userId = 1 AND 'end' < UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 1 WEEK))
like image 55
ceejayoz Avatar answered Nov 30 '22 17:11

ceejayoz


SELECT * FROM msc_calendar WHERE calendar_userId = 1 AND 'end' < UNIX_TIMESTAMP()-7*24*60*60
like image 42
Eugen Rieck Avatar answered Nov 30 '22 16:11

Eugen Rieck