PHP script to execute at a specific time

Is there an easy way to get php script to execute some html at specific time of the day?

For example, I have a headline on my homepage, and at certain points I want to be able to add something right under the heading, in this case an iframe.

I know everyone mentioned cron's work, but how does this work with this? is there also an alternative? It is not available on all hosting sites.

+6
php time
source share
8 answers

The idea of ​​cron and scheudled seems to contradict what you are actually trying to do. If you want something to be displayed (iframe in this case) only at certain points in time, you can simply check the server time during each request and select it if you are for a certain period of time.

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

<!-- 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 the conditional statement to show the iframe several times a day, or try a script to check which external condition you want to use to control the display of your iframe.

Update: Additional times or types of comparisons can be specified using

 <?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 an iframe from under your header with a server / task-side scheduler job is much more complicated than just conditionally displaying it during each request.

Even if you have a specific task that needs to be performed, for example, a periodically generated report, the actual work of displaying the results is usually independent of the periodic task. The PHP script is responsible for showing that the iframe will still query the database at the time of the request for any new content that will be displayed, and display it if it is found, and not a periodic task, modifying the script in some way to include the iframe .

+11
source share

If you are running Linux, you may have a cron job.

If you are on Windows, use Task Scheduler.

If you are in a hosted environment, you need to check if it is allowed.

+6
source share

cron on UNIX, launchd on OS X, and Task Scheduler on Windows platforms.

+6
source share

If you do not have access to cron jobs or scheduled tasks on your server, you can use online services like http://pingability.com/ to hit your script at regular intervals. This is not ideal, but you can create some kind of secret key and code that ensures that the script will not be run several times over a certain period of time. It may seem a bit hacky, but I used it on live systems to send daily emails and it has been working fine for over a year now.

+4
source share

On Unix systems, cron is your best bet.

+2
source share

Use a cron job or something similar, see fi cron jobs or the PHP scheduler

+2
source share

you can assign a task as a cron job.

0
source share

You can add a PHP script to your crontab to automatically run the script at specific intervals. At the command prompt, type crontab -e to add an entry to your crontab.

0
source share

All Articles