How to Run the PHP Script at Scheduled Time

How can I make my PHP script run at a certain time everyday?

What you want is called a cron job and is already covered here:

PHP: running scheduled jobs (cron jobs)

automatically schedule a php script execution at specific time

You have 3 options. My recommendation: Use cron if you can, user driven if you have to, and daemon as a last resort.

(1) cron (as mentioned in comments)

cron is a scheduler for linux systems that will run a command line job on your system. You log into your server over ssh, type crontab -e, and add a line like this:

4 5 * * * php /path/to/my/script.php

This would run the script at 5:04 a.m. every day.

<?php
// /path/to/my/script.php

// Do something

Some hosting services allow entering cron jobs with a GUI. There are also external cron services, that will call a URL for you at specific times.

(2) daemon

This is the most advanced option and also the least reliable: You run a command line script that contains an infinite loop. The script then periodically checks state and responds to it. Because it is likely to crash after months of running, you have to have a second script to restart it in case it does. This is a lot of work, but it is the most flexible approach.

<?php

while (1) {

// fetch $last_exec_timestamp from database

if ($last_exec_timestamp < time() + 86400) {
// set last_exec_timestamp to now in database

// do something
}
sleep(5);

}

3. user driven

If you have a decent amount of traffic on your site, you can simply include a the job this in your page footer, when there is no more output. Make sure this code is fast, or an unlucky user will be waiting for it.

<?php

// fetch $last_exec_timestamp from database

if ($last_exec_timestamp < time() + 86400) {
// set last_exec_timestamp to now in database
// do something
}

There are also to more fancy approaches of "user driven" that I haven't personally tested in another stack overflow question.

What options are there for executing a PHP script at a certain time every day?

Depends how exact the timing needs to be. A technique I've seen used (dubbed "poor man's cron") is to have a frequently accessed script (a site's home page, for example) fire off a task on the first page load after a certain time (kept track of in the database).

If you need any sort of guaranteed accuracy, though, cron or a Windows scheduled task is really the best option. If your host doesn't support them, it's time to get a better one.

PHP script to execute at certain times

The idea of cron and scheudled jobs seems to run counter to what you're actually trying to do. If you want something to display (an iframe in this case) only during certain times, you can simply check the server time during each request, and opt to display it if you're within a given time period.

Something like this will produce the same effect as a cron job, with more granularity, checking the time at the exact moment the requst is made.

<!-- Your Header here -->

<?php
$hour = date('G'); // 0 .. 23

// Show our iframe between 9am and 5pm
if ($hour >= 9 && $hour <= 17) { ?>
<iframe .... ></iframe>
<?php } ?>

You can expand on the conditional statement to show the iframe multiple times per day, or have your script check whatever external condition you're looking to use to govern the display of your iframe.

Update:
Additional times or types of comparisons could be specified via something like

<?php 
$hour = date('G');
$day = date('N'); // 1..7 for Monday to Sunday

if (($hour >= 5 && $hour <= 7) // 5am - 7am
|| ($hour >= 10 && $hour <= 12) // 10am - 12 noon
|| ($hour >= 15 && $hour <= 19) // 3pm - 7pm
|| ($day == 5) // Friday
) { ?>
<iframe...></iframe>
<?php } ?>

The idea of periodically adding/removing the iframe from below your header with a server-side cron/task scheduler job is far more complex than simply conditionally displaying it during each request.

Even if you have some specific task which must run, such as a periodically generated report, the actual job of displaying the results usually don't fall upon the periodic task. The PHP script responsible for showing that iframe would still query the database at the time the request is made for any new content to show, and display it if found, rather than the periodic task somehow modifying the script to include an iframe.

How do I run a PHP script using windows schedule task?

Locate the php.exe executable on your system and pass it the name of the script file using the -f parameter.

Example:

C:\Xampp\php\php.exe -f C:\Xampp\htdocs\my_script.php

Reference:

  • Introduction to using PHP on the command line
  • PHP command line options

How can I run a PHP Script automatically Daily in WAMP / Windows Environment?

You can do this with windows scheduler with command php full\link\to\php\file.php, if this not working probably link to php.exe file is not properly linked in systems PATH variable. So you can try then something like this C:\wamp\bin\php\php5.5.12\php.exe C:\wamp\www\test.php.

Also you can use at cmd command to set up schedule task, you can read more about it here

Solution:

PHP File:

<?php
$myfile = fopen("H:\\wamp\\www\\file\\newfile.txt", "w") or die("Unable to open file!");
$txt = "John Doe\n";
fwrite($myfile, $txt);
$txt = "Jane Doe\n";
fwrite($myfile, $txt);
fclose($myfile);
?>

BAT File:

H:\wamp\bin\php\php5.5.12\php.exe H:\wamp\www\file\file.php

Double Click BAT File will Create a newfile.txt.



Related Topics



Leave a reply



Submit