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.


$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!

1 Year
Discussion Span
Last Post by diafol

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


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.


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)
        elseif($now == $base)
            mail($to, $subject, $message, $headers);
            sleep(60); //In order to make the next execution == FALSE

Edited by James_43: mistake


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.