PHP script working cron after 2 weeks of sending a reminder to a specific email address

Ok, I have a php script that sends random code to the email address that the user entered.

I need to do this after 2 weeks, when the user receives a reminder to the same email address. This should happen only once.

I created a database table in which I store the user's email address and the date they were added.

How can I write a script so that after 2 weeks the user will be reminded by email.

I know that I need to use cron Job, my setup looks like this: enter image description here

As I understand it, mailform.php will be used every day at 23 oโ€™clock, right? And I need to have cron Job use the script every day to check if the user has gone 2 weeks or not.

My database table looks like this: enter image description here

Everything is in order, I just donโ€™t know how to check if two weeks have passed.

+4
source share
3 answers

The reason the data is not saved is because you are not actually saving it.

You need to call:

mysql_query($query); 

to write to the database.

In addition, in the cron job, the column "minute" should be "0". Thus, cron will work at 23:00 every day. If you put "*" in the minutes column, it will work every minute 23 hours, so 23:00, 23h01, 23h02, 23h03 ... etc.

You can find basic help on crontab here: http://crontab.org/

Also, as stated above, you should use mysqli_ functions or PDO modules if you can.

mysql_ functions have been deprecated since a very long time and are replaced by mysqli_ functions ("i" for improvement). They work very likely, and you do not have to change your code.

Good luck with your project.

+1
source

You can use the following in a script to determine if it was 2 weeks: Today = March 14, 2013

 <?php $regDate = "2013-02-28"; $today = date("Ymd"); $date = date("Ymd",strtotime("+2 weeks", strtotime($regDate))); if($today == $date) //do stuff if 2 weeks else //do stuff if isn't ?> 

You will need to change it to suit your needs, but there is a way to determine the dates.

How it works:

 strtotime("+2 weeks", strtotime($regDate)) 

This takes the line $regDate and makes time out of it. Then he will add 2 weeks and make time from a new time.

 date("Ymd", above); 

Then a date will be created that can be used to compare with $today

Please read the manual : PHP Date

+1
source

As I understand it, mailform.php will be used every day at 23 oโ€™clock, right?

No. You need 0 23 * * * /path/to/script , so it runs at 11pm every day.

This MySQL will provide you only those users who registered two weeks ago (so where regDate was exactly two weeks ago)

 SELECT * FROM `Your_Users_Table_Name` WHERE `regDate` = DATE_FORMAT(DATE_SUB(NOW(), INTERVAL -2 WEEK), '%Y-%m-%d') 

Then in your cron script, you can use this request so that your users then scroll through the lines, sending your reminder or code or something else.

Crude oil example

 $sql = " SELECT * FROM `Your_Users_Table_Name` WHERE `regDate` = DATE_FORMAT(DATE_SUB(NOW(), INTERVAL -2 WEEK), '%Y-%m-%d') "; if (false === ($result = mysqli_query($connection, $sql)) { trigger_error('there was a query/sql problem', E_USER_ERROR); } if ($result->num_rows > 0) { // ooo we found users from two weeks ago. while ($row = mysqli_fetch_row($result)) { // send email to user mail($row['email'], 'The email Subject', 'The email message body'); // update the users table and set remind = 1 if (false === (mysqli_query($connection, "UPDATE `Your_Users_Table_Name` SET `remind` = 1 WHERE `email` = '".$row['email']."'"))) { trigger_error('there was a query update problem!', E_USER_ERROR); } // sleep for 2 second so not to hammer mail systems and get flagged as abusive/spammer sleep(2); } mysqli_free_result($result); } else { // nothing to do today } 
0
source

Source: https://habr.com/ru/post/1469180/


All Articles