Forum Home
Press F1
 
Thread ID: 69364 2006-05-30 05:28:00 How to - Website pics vary with time of day... John W (523) Press F1
Post ID Timestamp Content User
459085 2006-05-30 05:28:00 Hi there

Is there a way I can alter the pic displayed on a Website, depending on the time of day on the Users PC?

Id like to display a new pic every hour from 9am through to 6pm, then retain the base/default pic from 6pm through to 8am the following day.

If we cant use the Users PC time, could we use the NZ server time. Server is Webbase.

Thanks..........John in Mosgiel.
John W (523)
459086 2006-05-30 06:44:00 I don't know how to do that, but would probably want to use PHP or ASP, depending on what your server is capable of.

If you want to try the random image script (php), here is one i've used in the past.. you can adapt it to suit:



<?

$load_picturename = rand(1,3);

echo "<img src=\"/file/location/picturename_".$load_picturename.".jpg\">";

?>


rename "picturename" to whatever image name you choose.
change "file/location..." to whatever file your images are located on teh server.
change(1,3) to (1,6) for example, if u want 6 random images..
all your images will need to be labelled the same except for the end number like this..
imagename_1, imagename_2 etc.

and simply place the code in HTML

..sorry if that didn't make much sense..hmm
jermsie (6820)
459087 2006-05-30 07:01:00 Thanks, but the hourly images are to display progress on a sport event, so random wont really do.

:-)
John W (523)
459088 2006-05-30 08:38:00 Glad to see another WB customer. :)

I suggest you use Javascript. With a quick google search I found this:

javascript.about.com
superuser (7693)
459089 2006-05-30 22:06:00 PHP will work fine. Just use the date() function to return the hour (24hr format):
<?php
$hour = date('G');
$output_filename = "filename_" . $hour . ".jpg";
echo("<img src=\"$output_filename\" />");
?>Just embed that in your HTML code. You can also do a lot of other fancy things with the date() function depending on your needs - see here (nz.php.net).

I suggest you use Javascript.Not such a good idea. Remember that a lot of users have Firefox/Scriptblock, and the site should degrade gracefully. PHP will allow this, Javascript won't.
Erayd (23)
459090 2006-05-30 22:18:00 OK I looked at the Javascript code, and now I really suggest you don't use it. It takes its time from the visitor's computer (not reliable) and uses some quite bulky code for the job it does. Much better to use the server time (you can rely on that not changing). Erayd (23)
459091 2006-05-30 22:29:00 Just realised you also want a default image between certain times . Try this one:
<?php
$time = date('H');
if ($time > 18 || $time < 9) $time = 'default';
else $time = date('d') . $time;
$output_filename = 'filename_' . $time . ' . jpg';
echo("<img src=\"$output_filename\" />");
?>Each file should be named something like 'filename_0213 . jpg', where '02' is the day, and '13' is the hour . The default image should be called 'filename_default . jpg' . You should also use 'H' rather than the 'G' I used in the earlier script - it makes naming the files easier, as 'H' has a leading zero where 'G' does not .
Erayd (23)
459092 2006-05-30 22:50:00 Very cool script. I can see a use for this that I never thought I wanted. Cheers for that. Greg (193)
459093 2006-05-31 00:12:00 I've never had JS issues with Firefox on Linux or Windows desktops .

Point taken though, the php solution looks much better .
superuser (7693)
459094 2006-05-31 00:30:00 Is there a way I can alter the pic displayed on a Website, depending on the time of day on the Users PC?

Snce you're usng the time on the USER'S PC, not the SERVER, you'll need to read the HTML headers. This can be unreliable, since it's possible block some with a proxy.

www.w3.org
kingdragonfly (309)
1 2