Daveed1973 Posted October 13, 2009 Share Posted October 13, 2009 Hi, I am new to these forums and would really appreciate some advice on a piece of coding I have done. Basically what I am trying to do is on our intranet, display a different home page banner depending on the time of the year. For example, display a halloween banner for halloween. So this is the code I have put in to do the trick but as I am no expert on PHP I was wondering if there was an easier way of doing it. $currday = date('d'); // gets the current day of the year $currmonth = date('m'); // gets the current month of the year if ($currmonth == '09') { // if in October, display Halloween banner if ($currday >= '12' && $currday <='31') { echo "<img src='images/buttons_banners/newsbar_banner_halloween.jpg' alt='Happy Halloween'/>"; } else { echo "<img src='images/buttons_banners/newsbar_banner_staff.jpg' alt='Welcome'/>"; } } elseif ($currmonth == '11') { // if in November, display bonfire night banner if ($currday >= '03' && $currday <='05') { echo "<img src='images/buttons_banners/newsbar_banner_bonfire.jpg' alt='remember remember'/>"; } else { echo "<img src='images/buttons_banners/newsbar_banner_staff.jpg' alt='Welcome'/>"; } } elseif ($currmonth == '12') { // if in December, display xmas banner if ($currday >= '01' && $currday <='31') { echo "<img src='images/buttons_banners/newsbar_banner_xmas.jpg' alt='Merry Christmas'/>"; } else { echo "<img src='images/buttons_banners/newsbar_banner_staff.jpg' alt='Welcome'/>"; } } else { // anything else, display default banner echo "<img src='images/buttons_banners/newsbar_banner_staff.jpg' alt='Welcome'/>"; } Any help would be appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/177541-solved-display-certain-image-depending-on-time-of-year/ Share on other sites More sharing options...
johnsmith153 Posted October 13, 2009 Share Posted October 13, 2009 If it works, great, but something like this may be very slightly better (but still only do the same job): $d = date('d'); // gets the current day of the year $m = date('m'); // gets the current month of the year $banner=""; if(m==9&&d>=12&&d<=31) { $banner=1;//1 == halloween } if(m==12&&d>=1&&d<=31) { $banner=2;//2 == xmas } if($banner=="") { echo "welcome banner"; } if($banner==1) { echo "halloween banner"; } if($banner==2) { echo "xmas banner"; } Quote Link to comment https://forums.phpfreaks.com/topic/177541-solved-display-certain-image-depending-on-time-of-year/#findComment-936124 Share on other sites More sharing options...
mikesta707 Posted October 13, 2009 Share Posted October 13, 2009 I would use a switch case statement, rather than a bunch of if elseif's $month = date("m"); switch($month){ case "09": //do october stuff break; case "12": //do december stuff break; default: //de default banner break; } Quote Link to comment https://forums.phpfreaks.com/topic/177541-solved-display-certain-image-depending-on-time-of-year/#findComment-936126 Share on other sites More sharing options...
GKWelding Posted October 13, 2009 Share Posted October 13, 2009 rather than using many else if's use a switch statement instead, much quicker, that way you can set banners up for the months you want then have your default case as the banner for in between events. Quote Link to comment https://forums.phpfreaks.com/topic/177541-solved-display-certain-image-depending-on-time-of-year/#findComment-936128 Share on other sites More sharing options...
johnsmith153 Posted October 13, 2009 Share Posted October 13, 2009 And October is month 10 (not 9). I'm sure you would have picked up on this before going live! Quote Link to comment https://forums.phpfreaks.com/topic/177541-solved-display-certain-image-depending-on-time-of-year/#findComment-936129 Share on other sites More sharing options...
GKWelding Posted October 13, 2009 Share Posted October 13, 2009 you will also probably want to use date('n') rather than 'm' as 'm' returns the month with leading zero's whereas 'n' doesn't. Quote Link to comment https://forums.phpfreaks.com/topic/177541-solved-display-certain-image-depending-on-time-of-year/#findComment-936132 Share on other sites More sharing options...
mikesta707 Posted October 13, 2009 Share Posted October 13, 2009 you can also use F or M to get textual representations of the month, if that floats your boat Quote Link to comment https://forums.phpfreaks.com/topic/177541-solved-display-certain-image-depending-on-time-of-year/#findComment-936135 Share on other sites More sharing options...
Daveed1973 Posted October 13, 2009 Author Share Posted October 13, 2009 That's brilliant, some good ideas there... many thanks for all your help Quote Link to comment https://forums.phpfreaks.com/topic/177541-solved-display-certain-image-depending-on-time-of-year/#findComment-936183 Share on other sites More sharing options...
mikesta707 Posted October 13, 2009 Share Posted October 13, 2009 there is a topic solved at the end of the posts if your topic has been solved Quote Link to comment https://forums.phpfreaks.com/topic/177541-solved-display-certain-image-depending-on-time-of-year/#findComment-936186 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.