How do I get Timestamp minus 6 weeks in MySQL?

MysqlTimestamp

Mysql Problem Overview


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

Mysql Solutions


Solution 1 - Mysql

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

Solution 2 - Mysql

where column>=date_sub(now(), interval 6 week)

Solution 3 - Mysql

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)

Solution 4 - Mysql

You can use between and now():

select somevalue 
from yourtable
where yourtimestamp between now() - interval 1 day and now()

Solution 5 - Mysql

for TIMESTAMP there is a TIMESTAMPADD() function

SELECT TIMESTAMPADD(WEEK, -6, CURRENT_TIMESTAMP)

this will return the timestemp of 6 weeks ago

or in the case like the question

SELECT * FROM users
WHERE lastlogin > TIMESTAMPADD(WEEK, -6, CURRENT_TIMESTAMP)

Solution 6 - Mysql

Any luck yet. Have you tried:

>= DATE_SUB(NOW(), INTERVAL 6 WEEK) 

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionsark9012View Question on Stackoverflow
Solution 1 - Mysql700 SoftwareView Answer on Stackoverflow
Solution 2 - MysqlajrealView Answer on Stackoverflow
Solution 3 - MysqlNeilView Answer on Stackoverflow
Solution 4 - MysqlEric LeschinskiView Answer on Stackoverflow
Solution 5 - MysqlIsaac WeingartenView Answer on Stackoverflow
Solution 6 - Mysqlrd42View Answer on Stackoverflow