dawnrae Posted March 24, 2007 Share Posted March 24, 2007 Hello, I've got a site I'm working on and I want a picture in the header to change according to the time of day, based on Central Standard Time. I've got a script that I found and I've been working with it, but it's not working correctly. <?php $timeOffset = 0; // in hours, positive or negative $nowHour = date("H", time() + (3600 * $timeOffset)) + 0; if ($nowHour >= 7 && $nowHour < 9) $nowImg = 'austin-morning.jpg'; else if ($nowHour >= 9 && $nowHour < 19) $nowImg = 'austin-day.jpg'; else if ($nowHour >= 19 && $nowHour < 20) $nowImg = 'austin-evening.jpg'; else if ($nowHour >= 20 && $nowHour <=6) $nowImg = 'austin-night.jpg'; else $nowImg = 'austin-day.jpg'; ?> I'm pretty much PHP challenged except for incorporating scripts and making little edits here and there. Can anyone help me understand what's wrong here and why, even though my server time is CST, that the images are not showing up as they should? Thank you in advance for any help anyone can offer. Dawn Quote Link to comment https://forums.phpfreaks.com/topic/44071-help-change-picture-at-time-of-day-with-php/ Share on other sites More sharing options...
MadTechie Posted March 24, 2007 Share Posted March 24, 2007 What is going wrong ? script seams fine.. if its not displaying the image then your need to add more of the code Quote Link to comment https://forums.phpfreaks.com/topic/44071-help-change-picture-at-time-of-day-with-php/#findComment-214045 Share on other sites More sharing options...
jitesh Posted March 24, 2007 Share Posted March 24, 2007 Mention ur whole code here. need to review whats wrong with it. Quote Link to comment https://forums.phpfreaks.com/topic/44071-help-change-picture-at-time-of-day-with-php/#findComment-214138 Share on other sites More sharing options...
obsidian Posted March 24, 2007 Share Posted March 24, 2007 Hello, I've got a site I'm working on and I want a picture in the header to change according to the time of day, based on Central Standard Time. I've got a script that I found and I've been working with it, but it's not working correctly. <?php $timeOffset = 0; // in hours, positive or negative $nowHour = date("H", time() + (3600 * $timeOffset)) + 0; if ($nowHour >= 7 && $nowHour < 9) $nowImg = 'austin-morning.jpg'; else if ($nowHour >= 9 && $nowHour < 19) $nowImg = 'austin-day.jpg'; else if ($nowHour >= 19 && $nowHour < 20) $nowImg = 'austin-evening.jpg'; else if ($nowHour >= 20 && $nowHour <=6) $nowImg = 'austin-night.jpg'; else $nowImg = 'austin-day.jpg'; ?> I'm pretty much PHP challenged except for incorporating scripts and making little edits here and there. Can anyone help me understand what's wrong here and why, even though my server time is CST, that the images are not showing up as they should? Thank you in advance for any help anyone can offer. Dawn Dawn, my first thought is that you may not be actually displaying the image. This script is simply creating a variable to hold the source for your image tag. Make sure you're actually displaying the image elsewhere on your page: <img src="/path/to/images/<?php echo $nowImg; ?>" /> Otherwise, as the others have said, it looks pretty good. Hope this helps! Quote Link to comment https://forums.phpfreaks.com/topic/44071-help-change-picture-at-time-of-day-with-php/#findComment-214182 Share on other sites More sharing options...
AndyB Posted March 24, 2007 Share Posted March 24, 2007 else if ($nowHour >= 20 && $nowHour <=6) er, what number can be more than 20 AND less than 6?? Perhaps that line needs to be OR else if ($nowHour >= 20 || $nowHour <=6) Quote Link to comment https://forums.phpfreaks.com/topic/44071-help-change-picture-at-time-of-day-with-php/#findComment-214186 Share on other sites More sharing options...
dawnrae Posted March 24, 2007 Author Share Posted March 24, 2007 Thank you for your replies. I think maybe your advice has helped. I made a couple changes, adding the || to the last else if statement: <?php $timeOffset = 1; // in hours, positive or negative $nowHour = date("H", time() + (3600 * $timeOffset)) + 0; if ($nowHour >= 7 && $nowHour < 9) $nowImg = 'austin-morning.jpg'; else if ($nowHour >= 9 && $nowHour < 19) $nowImg = 'austin-day.jpg'; else if ($nowHour >= 19 && $nowHour < 20) $nowImg = 'austin-evening.jpg'; else if ($nowHour >= 20 || $nowHour <=6) $nowImg = 'austin-night.jpg'; else $nowImg = 'austin-day.jpg'; ?> And also to the call within the page, where I'd initially had () around the $nowImg: <?php echo $nowImg; ?> And it seems to be working now. I'm going to keep track of it throughout the day to make sure. Thank you all so much for helping out my php-challenged self. I truly appreciate it. If anyone else sees anything problematic in the code, feel free to chime in. I am grateful for all the advice I can get . I'll let you know how it goes! Dawn Quote Link to comment https://forums.phpfreaks.com/topic/44071-help-change-picture-at-time-of-day-with-php/#findComment-214206 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.