Moderator's note: please try to use more descriptive titles when posting. You're in the PHP help section, so we know you want help with PHP. In this case, you should probably have mentioned something about using dates/times in PHP. That will actually benefit you-- more users will view your post and answer your question because they know what it's about, plus the fact that others can later find the discussion if they have similar questions.
There's one problem with this: PHP only determines what happens when the page loads (it just generates an HTML page), so you can have PHP show one image at one time of day, but it won't disappear after that minute. I don't know if this is important to you. (Note that in theory you would use Javascript to hide it after that one minute, but it wouldn't be secure, because unlike PHP, Javascript operates within the browser and can be modified by the user if they know what they're doing or take the time to figure it out.)
Your code looks fine to me. Have you tested it to be sure that the image commands are working? That's the first step.
Next:
http://www.php.net/manual/en/function.date.php
Your use of H:i
looks fine. What this means is that at 11:10am at server time you will display one image. Any other time, you'll display another.
It's technically possible to create code that will find the UTC offset and calculate the difference for you. However, in this case (unless you want the code to be portable for other machines) it will be simpler to just use trial and error to figure out where the machine is.
Start with this:
echo date("H:i");
Then compare that to your local time and/or UTC, and you'll know where the server is located (or what timezone it is set to-- probably where it's located). Then just add/subtract the right number of hours (the minutes should be ok), and the code should work.
Note: when using 'echo' you won't see anything if you have an image being displayed (and/or it will cause errors on the page and no image to be shown). For that reason, you should probably comment out the end of your code while testing. And instead, you can replace the whole thing with code like this:
PHP Code:
<?php
$datetime = date("H:i");
echo $datetime;
if ( $datetime == "11:10" ) {
//$im = imagecreatefromgif("image2.gif");
echo 'image2';
} else {
//$im = imagecreatefromgif("image1.gif");
echo 'image1';
}
/*header("Content-Type: image/gif");
imagegif($im);
imagedestroy($im);*/
?>
(Also, note for the future that you can use [php] code tags to format it nicely for us here, or just [code] for other kinds of code.)
Bookmarks