Shaun
Shaun

Reputation: 2181

How do check for accounts older than a certain date in mysql and php?

I'm setting up a cleanup on accounts in my database, and only want to run it on accounts that haven't been checked for cleaning in the last 3 months.

Once an account has been checked, regardless if it gets cleaned, I save the date that it's been checked in the following format eg 2019-05-29

When checking for accounts that need to be cleaned, how do I write the

WHERE `accountLastCheckedDate` > 3 months 

part of the query please?

Thank you for your time and help.

Upvotes: 1

Views: 46

Answers (3)

Booboo
Booboo

Reputation: 44213

If you are interested in date and time:

WHERE `accountLastCheckedDate < `DATE_SUB(NOW(), INTERVAL 3 MONTH);

If you are interested in just the date:

WHERE `accountLastCheckedDate` < DATE_SUB(CURDATE(), INTERVAL 3 MONTH);

Upvotes: 1

forpas
forpas

Reputation: 164139

You want accountLastCheckedDate older than today minus 3 months

SELECT * FROM tablename
WHERE accountLastCheckedDate < DATE_ADD(CURDATE(), INTERVAL -3 MONTH)

or newer?

WHERE accountLastCheckedDate >= DATE_ADD(CURDATE(), INTERVAL -3 MONTH)

Upvotes: 1

prince nhiel cada
prince nhiel cada

Reputation: 74

This will help you, gl hf

Select * FROM My Table where columname>=DATEADD(m, -3, GETDATE())

Upvotes: 1

Related Questions