0
votes

I have a php file that will run on Monday and Tuesday only any time between 9 to 10 AM.

The php file is controlled via Cron jobs and I have a batch file that will run every 2 minutes and send mail ( using task schedular I'm running the bat file every 2 minutes)

The problem is:

The if condition gets satisfied between 9 to 10 AM and send the mail repeatedly. I want the myMondayTuesdayMailList() function to run only once a day by setting some flag or by someway to handle this

In other words, there are plenty of functions running inside send_mail.php and the function myMondayTuesdayMailList() should be executed only once on Monday and Tuesday only even if the page is refreshed/reloaded via cron again and again ( I know I need to set some flag or variable but I'm blank now. Help me out).

How can I achieve this

send_mail.php

    <?php
    $get_day = date('D');
    $get_time = date('H');
    if(($get_day == 'Mon' and $get_time >=9 and $get_time <= 10) || ($get_day == 'Tue' and $get_time >=9 and $get_time <= 10)){
    {
    myMondayTuesdayMailList();
    }

    function myMondayTuesdayMailList(){
    ....... my query and php stuffs to send mail..........
    }

    function testfunction1(){

    }

    function testfunction2(){

    }
    ?>

cron.bat

start "mailinglist" "C:\xampp\php\php.exe" -f C:\xampp\htdocs\test\send_mail.php

2
after the function is executed you must store some flag/date into database or a flat file (eg .txt) and in code you must always check before executing the function whether date is set or not....In else part of your code you can again reset the value - undefined_variable

2 Answers

-1
votes

Try this:

$once_t=false; 
if(($get_day == 'Mon' and $once_t==true and $get_time >=9 and $get_time <= 10) || ($get_day == 'Tue' and $get_time >=9 and $get_time <= 10)){
 myMondayTuesdayMailList();
}

function myMondayTuesdayMailList(){
....... my query and php stuffs to send mail..........
$once_t=true;
}
-1
votes

you can simply use windows task scheduler in windows and crontab in linux rather than deciding to send email from php.