问题
Basically, am creating a mailer system to send a mail to my inactive members to do activities in their account if they have not logged in since 30 days.
My previous login date stores in my database in unixtimestamp hence I coded as below.
select email from myusers where WHERE DATE_ADD(FROM_UNIXTIME(prelogin), INTERVAL 30 DAY) < CURDATE()";
But this code after 30 days it is sending continuos mails almost everyday as I have set cron to run once a day.
回答1:
Assuming you only want an email sent every 30 days, and only if they haven't logged in for 30 days, I suspect the easiest solution will be to have another column in your table called something like last_email_sent_time
.
Then, only send an email if it's been at least 30 days since both prelogin
and that new column.
And, when you send the email, update that new column to the current date so you won't bother them for another month or so.
On the other hand, if you just want the email sent once after they haven't logged in for thirty days, it's a similar thing.
Create a new column indicating the email has been sent and only send it if that column is false (or N
or whatever you want to use to indicate it). Sending it will set that column to true, logging in will set it to false (as well as changing prelogin
).
If you don't want to add extra columns (for some bizarre reason), you could simply update prelogin
when you send the email, though that's not something I'm comfortable suggesting since it loses information that may be used (or useful) elsewhere.
来源:https://stackoverflow.com/questions/10384550/need-help-on-unix-time-stamp