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!

Member Avatar for diafol

You need to set up a cron job through your cPanel

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

Member Avatar for RudyM

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.

Member Avatar for diafol

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.

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.

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.

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
        }
    }
Member Avatar for diafol

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.