user240993
user240993

Reputation:

PHP script to execute at certain times

Is there a simple way to have a php script execute some html at a certain times of the day?

For example i have on my home page a header and at certain times i want to be able to add something right under the header, in this case a iframe.

I know everyone mentioned cron jobs but how would this work with that? also is there an alternative? Its not available on all hosting

Upvotes: 8

Views: 25022

Answers (8)

user229044
user229044

Reputation: 239491

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.

Upvotes: 18

sjobe
sjobe

Reputation: 2837

When you don't have access to cron jobs or scheduled tasks on your server, you can use online services such as http://pingability.com/ to hit your script at specified intervals. It is not perfect, but you can build in some kind of secret key and code that makes sure that the script doesn't get run multiple times within a certain time period. Might seem a little hacky, but I've used it on live systems to send out daily emails and it's been working fine for over a year now.

Upvotes: 5

dekko
dekko

Reputation: 11

You could add a PHP script to your crontab to automatically run the script at defined intervals. From the command line, enter crontab -e to add the entry to your crontab.

Upvotes: 1

Manjula
Manjula

Reputation: 5101

you can schedule the task as a cron job.

Upvotes: 0

pix0r
pix0r

Reputation: 31290

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

Upvotes: 6

jeffa00
jeffa00

Reputation: 677

If you are running on Linux, then you could have a cron job.

If you are on Windows, then use Task Scheduler.

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

Upvotes: 6

Wim
Wim

Reputation: 11252

Use a cron job or something similar, see f.i. cron jobs or PHP scheduler

Upvotes: 2

Darrell Brogdon
Darrell Brogdon

Reputation: 6973

On Unix systems cron is your best bet.

Upvotes: 2

Related Questions