tags:

views:

627

answers:

8

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

+1  A: 

On Unix systems cron is your best bet.

Darrell Brogdon
+1  A: 

Use a cron job or something similar, see f.i. http://stackoverflow.com/questions/1268044/cron-jobs-or-php-scheduler

Wim
+6  A: 

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.

jeffa00
If neither is allowed on the (v)host, you can write a command line PHP script that runs as a daemon and executes the scripts at configured times. A last resort.
fsb
Or you could make a webservice that triggers it and call that from a box you do own.
jeffa00
+5  A: 

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

pix0r
A: 

you can schedule the task as a cron job.

Manjula
A: 

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.

dekko
+1  A: 

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.

sjobe
I tried it but the link seems to be down
delboud
http://www.siteuptime.com/ and https://www.websitepulse.com/ seem to offer identical services.
sjobe
+1  A: 

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.

meagar
Exactly what I was looking for but I only do front end so im still kinda lost on adding extra times in the scrip.
delboud
Perfect!! Thanks a lot!!
delboud