JamesG
JamesG

Reputation: 2018

MySQL Interval - bring in users that have not logged in for 6 months

I have a simple query:

    $query1="SELECT * FROM wp_users WHERE now() < (last_login + INTERVAL 6 month)";
$result1=mysql_query($query1) or die(mysql_error());

while($rows=mysql_fetch_array($result1)){  

    $inactive_accounts[] = $rows['ID'];
}

I want it to pull in all user_id's of people who have not logged into their account for 6 months. I have it set so when they log in, it inseerts datetime into the last_Login column.

I just wondered if this SELECT query was ok, as I am having trouble testing it. Will this show ID's of people who have not logged in for 6 months?

Thanks!

Upvotes: 1

Views: 148

Answers (3)

Michael Slade
Michael Slade

Reputation: 13877

To test that your code works, manually tweak a user's last_login field and set it to > 6 months ago. In your development environment of course!

Upvotes: 0

fancyPants
fancyPants

Reputation: 51888

SELECT * FROM wp_users WHERE last_login < date_sub(now(), INTERVAL 6 month)

Upvotes: 3

liquorvicar
liquorvicar

Reputation: 6106

You're on the right lines. What you probably need is something like the DATE_SUB function in MySQL, so

SELECT *
FROM wp_users
WHERE last_login < DATE_SUB( NOW(), INTERVAL 6 MONTH )

Upvotes: 4

Related Questions