0

Anybody help me ?

I need a php script that will run automatically every day on Morning 09:00 O'clock. But my script not working. My script.

<?php
date_default_timezone_set('Asia/Calcutta');

$now = date("h:i");
$base = '09:00';
$to      = 'test@mydomain.com';
$subject = 'My Subject';
$message = 'Hello';
$headers = 'From: test@mydomain.com' . "\r\n" .
    'Reply-To: test@mydomain.com' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

if ($now == $base) {
    mail($to, $subject, $message, $headers);
}
?>

Thanks in advance!

7
Contributors
9
Replies
46
Views
1 Year
Discussion Span
Last Post by diafol
0

I don't want to use a cron job here.
I want to execute this code just PHP script.
Is this possible ?

0

Can you give more details regarding the source from where this will be executed? I used to schedule PHP scripts in a Windows Server environment using the task scheduler. If this is the case, you can first add the PHP path to the PATH system environment variable; this will allow you to use php as a command in the command-line. Then in the scheduler you can PHP C:\SCRIPTS_FOLDER\SCRIPT_FILENAME.php to execute.

Edited by RudyM: Added detail for clarification.

0

A windows task runner has the same function as a cron job. Why do you not want to use a cron job? That.s what they.re for.

0

PHP is executed when there's a request. Normally the first request comes from the browser when the user type the url and hit enter.

Cron jobs and Windows tasks acts like an hidden user that will request that URL at that time, making it execute whenever it's setted to.

If you want to do it ONLY with PHP you'll need to leave a tab on your browser open forever, and the PHP of the page will stay redirecting to itself forever.

It works? It does but if you close your tab it'll stop.

0

PHP 5.5.4 and later have the ability to run php code directly, even as its own web server (not necessary here I think). Anyway, what RudyM said. On Linux you would use cron to do this. It doesn't have to be a script - it can be in its own cron file in /etc/cron.d, as opposed to using /etc/cron.daily which will require a shell script to run the code.

0

The consensus here is on point. Your orginal code only checks once to see if the date is within the parameters or not, so in order for this to work you need to execute the script every minute (via a cron job).

Theoretically, you could use a sleep(60) command and ask the script to check again in a loop, but it's terribly inefficient since you know the sript only needs to be executed once every day. Still, if you are bent at doing it within a single execution, it's the only way I know.

$break = FALSE;

while($break == FALSE)
    {
        if($now != $base)
        {
            sleep(60);
        }
        elseif($now == $base)
        {
            mail($to, $subject, $message, $headers);
            sleep(60); //In order to make the next execution == FALSE
        }
    }

Edited by James_43: mistake

0

OK, summary - if on Linux, use cron. If on windows, use task. If you want to use pure PHP code - you're insane.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.