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

2
  • I'm sorry, how does a script 'execute some html'? Commented Dec 30, 2009 at 16:28
  • 2
    @Ben I think he means "generate" some html. Commented Dec 30, 2009 at 16:33

8 Answers 8

18

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Exactly what I was looking for but I only do front end so im still kinda lost on adding extra times in the scrip.
6

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.

2 Comments

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.
Or you could make a webservice that triggers it and call that from a box you do own.
6

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

Comments

5

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.

1 Comment

siteuptime.com and websitepulse.com seem to offer identical services.
2

On Unix systems cron is your best bet.

Comments

2

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

Comments

1

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.

Comments

0

you can schedule the task as a cron job.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.