Jump to content

Recommended Posts

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.

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";
}

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;
}

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.