How to Make My PHP Script Run at a Certain Time Everyday

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)

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.

Run php script every day

you can write your query and/or script in a function
then use the cronjob on the server to call this function every certain time
so for example if you are using ssh
do the following

$ crontab -e

this command will open the cronjob page to list all the cronjobs you have
then enter the following line

0 1 * * *  path_to_your_function

then save and close

0 1 * * *
it means everyday at 1 o'clock the server will call the function that has the queries or the php script you wrote

you can customize the cronjob timing and test it here

i would also like to recommend you to check the answer on Scheduling php script in phpMyAdmin 3.3.8.1

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.

How to run php script at 12am everyday

If you have access to the host you're running on and it's a UNIX-like machine, using cron would be your best bet.

How can I run PHP script in certain interval (e.g. once a day)?

If you can't or don't want to use use cron and it's ok to update it only when the page is accessed. You could cache the result of the HTTP request and only update it on a page load it if the cache is older than a day or whatever interval you choose.



Related Topics



Leave a reply



Submit