Archive - Originally posted on "The Horse's Mouth" - 2005-12-26 13:54:05 - Graham Ellis
"I want to show one picture on my website in the morning and a different one in the afternoon".
Doesn't that sound like it's a simple request ... yet - as ever with computing - it raises a whole lot more questions, with a raft of possible answers.
* Do you want the time from the server? Where's your server? One of ours is in California, and the other is in Germany! Or would you prefer the time to reflect where your main customer base is (the UK), or where people are browsing from?
* Do you want to change your HTML every 12 hours to pull in the new image, or to have a URL for the image that changes? Or do you want to run a timed job on the server to switch the image for you? Does it matter??
If you're looking for server or main customer base time to be reflected in the image, the easiest way is to use PHP for the image generation. Here's code to choose between two images based on the server's hour:
1. Changing to a different (fixed) timezone is quite easy - use either the date or gmdate functions from PHP and add or subtract an appropriate offset.
2. Timing by browser is a little more tricky. I would identify the country using Maxmind and then work with a table of time zones such as those here and here. Even then, you'll have daylight saving to take into account ...
3. If you prefer to keep your HTML fixed, you can use PHP to generate your image - example (to display a random image) here on our forum.
4. A further variation if you want to change the image but use static images and pages is to use a regular timed job (crontab) to change the file on the server at the correct times.