0

I have a mysql table and I have createDate column in it. I have to send email to the records after every 1 day passed.

For example I have two records and its dates :

2018-04-02 11:04:03
2018-04-04 09:01:45

The first record should get email when every 1 day passed. Hours, minutes are important, too.

For the first record, the dates of email times have to be =>

2018-04-03 11:04
2018-04-04 11:04
2018-04-05 11:04
...
...
...

And For the second record, the dates of email times have to be =>

2018-04-05 09:01
2018-04-06 09:01
2018-04-07 09:01
...
...

How can I handle this situation ?

Thanks all for your answers.

EDIT : I'm changing my question after comments.

I have a dates array.

$dates = ['2018-04-02 11:04:03', '2018-04-04 09:01:45'];

I want a function that will return exactly date difference with currentDate. The cron will work every minute and will check the difference.

 <?php

 function difference($currentDate, $date){
     //will return true if 1 day or 2 days or 3 days etc passed ...
 }    

$dates = ['2018-04-02 11:04:03', '2018-04-04 09:01:45'];

foreach($dates as $date){

    if(difference($currentDate, $date)){
        //send email
    } else {
        //dont send email
    }
}
ydlgr
  • 57
  • 1
  • 6
  • Is that for a realtime scenario? – ßiansor Å. Ålmerol Apr 04 '18 at 06:46
  • yes @ßiansorÅ.Ålmerol – ydlgr Apr 04 '18 at 06:52
  • 1
    @ydlgr check [this](http://sandbox.onlinephpfunctions.com/code/b20724302f38b1e782a531d0f03a0f8ddc43ecdb) is what you want? – Dmytro Apr 04 '18 at 07:33
  • Thank you @Dmitry. This is exactly what I want. Perfect ! – ydlgr Apr 04 '18 at 07:39
  • 1
    @ydlgr better and even maybe simpler would be to use separate storage for jobs which has only time instead of date. And when *createDate* is coming, you can add this date task to job with same time. With this approach you can group your tasks into one job and don't scan past dates in your table. I hope I clearly described the process :) – Dmytro Apr 04 '18 at 07:42
  • Yes @Dmitry , It will be simpler and I will use seperate storage :) Thanks again for your answer. – ydlgr Apr 04 '18 at 08:06
  • Run your php script via scheduler. If you using windows you can use task scheduler like this [How do I run a PHP script using windows schedule task? ](https://stackoverflow.com/questions/4701861/how-do-i-run-a-php-script-using-windows-schedule-task) to execute your code on specific time range and use CRON if you are using linux. – ßiansor Å. Ålmerol Apr 04 '18 at 06:51

1 Answers1

0

Does every second count? You could use a CronJob that runs a script (every minute or every second) that checks the dates.

EDIT:

<?php
$firstDate = "2018-04-03 11:04";
$secondDate = "2018-04-04 11:04";
$seconds = abs(strtotime($secondDate) - strtotime($firstDate));

if($seconds > 86340 && $seconds < 86460){
    echo "Almost exactly one day difference (± 1 minute tolerance)";
} else {
    echo "More or less than one day difference";
}
?>
David
  • 2,898
  • 3
  • 21
  • 57
  • Hi David. That is not my question. The cron will work every minute. My question is I need function which will calculate day difference and will return true or false. Can you check my question again ? – ydlgr Apr 04 '18 at 07:15
  • And what is the difference with such query? – Nico Haase Apr 04 '18 at 07:17
  • @ydlgr I updated my code. Is this what you want? – David Apr 04 '18 at 08:07