2

I have built a webapplication where a user pays for 1/3/6 months and joins the site , i need to send the user a reminder mail before 15 days of his account expiration , how can i achieve it ? i am not understanding correct logic ... i am storing the registered date , expiring date in database ,will this below logic work fine ?

<?php
$expiringDate = "2015-07-21";
$todaydate = date("Y-m-d");
$date = date("Y-m-d",strtotime("-15 days", strtotime($expiringDate)));
if($todaydate == $date){
//send mail
}else{
//quit
}?>

and also i want to change a value in database if today is expiring day ... is it better to do it in other cronjob or can i do it in the above code like this..

 <?php
$expiringDate = "2015-07-21";
$todaydate = date("Y-m-d");
$date = date("Y-m-d",strtotime("-15 days", strtotime($expiringDate)));
if($todaydate == $date){
//send mail
}else{
//check and change the value if today is the expiring
    }?>

am i going in the right path and is this secure or is there any other better way to do this job

4
  • Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise. I would suggest that you find a development forum (perhaps reddit?) to work out generalities. Then, when you have specific coding issues, come back to StackOverflow and we'll be glad to help. May 29, 2015 at 19:23
  • ok sir , atleast is this secure ? May 29, 2015 at 19:25
  • What do you mean by "secure"? May 29, 2015 at 19:26
  • Agreed with @JayBlanchard. There seems to be some other gotchas with your approach. You are using $todaydate == $date but have you considered what would happen if for some reason your cron job failed? You probably want to check $todaydate > $date and then a second condition would check if a reminder has already been sent. May 29, 2015 at 19:41

1 Answer 1

2

I would suggest running the cron job every day.

Your PHP script should then check for all people who expire in 15 days.

However as someone else pointed out, if your cron job fails one day you might miss out a batch of people.

Therefore I would check for people with 15 days or less remaining, that don't have a reminder flag set against them in your database. This means if the cron job failed for someone with 15 days remaining, then on the 14th/13th/12th, etc remaining day your script will see the reminder flag isn't set against them and will still send the reminder.

<?php
$reminderSent = false; // Get this value from the db (true or false)
$expiryActioned = false; // Get this value from the db (true or false)
$expiringDate = strtotime('2015-07-21'); // Get this date from the db
$todayDate = time();
$reminderDate = strtotime("-15 days", $expiringDate);
if ($todayDate >= $reminderDate && $reminderSent == false) {
    // Send mail
    // Set flag $reminderSent in database to indicate reminder has been sent
} elseif ($todayDate >= $expiringDate && $expiryActioned == false) {
    // Do something
    // Set $expiryActioned in database to indicate the user has expired and something has been done about it
}
?>

However, rather than select everyone and run through them all with the above logic, I would build the above logic into your SQL query for better efficiency.

Quick example:

// Select all users that expire in 15 days or less
SELECT `userid`, `name`, `email` FROM `user` WHERE NOW() >= DATE_SUB(`expiry`, INTERVAL 15 DAY) AND reminder_sent = 0

// Now loop through each user, send them an email and then:
UPDATE `user` SET reminder_sent = 1 WHERE `userid` = X

and

// Select all users that have expired
SELECT `userid`, `name`, `email` FROM `user` WHERE NOW() >= `expiry` AND `expiry_actioned` = 0

// Now loop through each user, do whatever you need to and then:
UPDATE `user` SET expiry_actioned = 1 WHERE `userid` = X

Not the answer you're looking for? Browse other questions tagged or ask your own question.