Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get Timestamp minus 6 weeks in MySQL?

I have a field named timestamp. This is the last time a member was logged in. I am looking to include a where clause in a query for something like

WHERE timestamp > todays date - 6 weeks

How would I do this? I am trying to only include users that have logged in in the last 6 weeks.

Thanks

like image 866
sark9012 Avatar asked Feb 23 '11 13:02

sark9012


People also ask

Can I subtract dates in MySQL?

Definition and Usage. The DATE_SUB() function subtracts a time/date interval from a date and then returns the date.

How do I subtract hours from a timestamp in SQL?

MySQL SUBTIME() Function The SUBTIME() function subtracts time from a time/datetime expression and then returns the new time/datetime.

How can I get the difference between two timestamps in MySQL?

To calculate the difference between the timestamps in MySQL, use the TIMESTAMPDIFF(unit, start, end) function. The unit argument can be MICROSECOND , SECOND , MINUTE , HOUR , DAY , WEEK , MONTH , QUARTER , or YEAR .


4 Answers

I find this syntax more readable than date_sub, but either way works.

WHERE timestamp >= NOW() - INTERVAL 6 WEEK 

If you want to go by "Today" (midnight) instead "now" (current time), you would use this

WHERE timestamp >= DATE(NOW()) - INTERVAL 6 WEEK 
like image 88
700 Software Avatar answered Oct 23 '22 02:10

700 Software


where column>=date_sub(now(), interval 6 week) 
like image 21
ajreal Avatar answered Oct 23 '22 02:10

ajreal


This link demonstrates how you might acquire a timestamp of yesterday using the format DATE_ADD(CURDATE(), INTERVAL -1 DAY), therefore your query would probably be:

WHERE timestamp > DATE_ADD(CURDATE(), INTERVAL -42 DAY)
like image 24
Neil Avatar answered Oct 23 '22 02:10

Neil


You can use between and now():

select somevalue 
from yourtable
where yourtimestamp between now() - interval 1 day and now()
like image 41
Eric Leschinski Avatar answered Oct 23 '22 01:10

Eric Leschinski