Displaying an image based on the date

A while back I found a script for displaying different images based on the date…problem being that a few weeks ago I had a problem with one of my hard drives and lost a load of files, and I fear this script was one of the ones amongst them, as I can’t find it anywhere. I can’t remember where I originally found the script though. :frowning: I’ve looked all over online and can’t find it again. I thought maybe it was here, but after searching around I can’t find it - I’ve asked and checked in various places, but haven’t been able to find it again.

The script allowed you to displaying an image based on the date, and would allow you to display different images on specific dates, or between selected dates, and display a default date if the current date wasn’t one listed with a specific image to display. It specified just the date and month so that it’s not year specific and is reuseable.

On another forum I got this, which is the closest to what I remember of the original script:

switch(date('m-d')) {
  // multiple dates with same image
  case '02-15':
  case '02-07':
    print '<img src="/path/to/image.jpg" alt="" />';
    break;
  // really long ranges don't work all that well in this solution
  case '03-01':
  case '03-02':
  case '03-03':
  case '03-04':
  case '03-05':
  case '03-06':
  case '03-07':
  case '03-08':
  case '03-09':
  case '03-10':
    print '<img src="/path/to/image.jpg" alt="" />';
    break;
  // specific date image
  case '12-25':
    print '<img src="/path/to/christmas.jpg" alt="" />';
    break;
  // fallback image
  default:
    print '<img src="/path/to/default.jpg" alt="" />';
    break;
}

The problem is (and I can’t remember how it was done in the original script) is that I can’t work out how to specify that an image should be displayed between to dates doing it with switch/case. I remember the original script was very much like the one above, except that the case statement was something along the lines of:

case '03-01' ?????????? 04-05':

(obviously the ‘???’ represents the bit I can’t remember). I’ve tried:

('01-25','01-31'):

…but it doesn’t like that at all, and:

case ('01-25'>='01-31'):

…which worked for the first date, and I thought I’d got it working, but then didn’t work for any subsequent dates or date ranges. I particularly liked the switch/case method because it seemed quite compact, and less code than using if/else/elseif, as I’ve quite a few individual dates and date ranges to list - as the dates are cancer awareness months, days, and other events run by medical charities, and there’s quite a few of them!

Has anyone any idea how I can specify a date range using this sort of format?

‘switch’ statement is using an exact comparison (no ranges are allowed) so I am afraid you are ‘stuck’ with if/elseif/else…

Hmmm…okay, thanks. That explains why what I was trying to do wasn’t working! :lol:

Though I have seen other ways of doing it, including a foreach loop and GD, I stuck with the if/elseif/else in the end. It’s probably not the most efficient way code-wise of doing it, but this worked in the end (part of the code anyway - it’s a very long list, and I won’t bore you with all of it!):


<?php 
	// Macmillan Cancertalk week (21-25 Jan)
	if ((date('m') == 01) && (date('d') >= 21) || (date('m') == 01) && (date('d') <= 23)) {
	echo "<img src=\\"images/ribbons/cancertalk.gif\\" height=\\"145\\" width=\\"175\\" alt=\\"Macmillan Cancertalk\\" /><br /><h6 class=\\"awareness\\">Macmillan Cancertalk Week <span class=\\"morelink\\"><a href=\\"the-bookstall-cancer-links-and-resources.php\\">more...</a></span></h6>";
	}	
	// Macmillan Cancertalk week (21-25 Jan) and Cervical Cancer Awareness Week (24-30 Jan)	
	else if ((date('m') == 01) && (date('d') == 24)) {
	echo "<img src=\\"images/ribbons/macmillan_cervical.gif\\" height=\\"145\\" width=\\"175\\" alt=\\"Macmillan Cancertalk and white and teal awareness ribbons\\" /><br /><h6 class=\\"awareness\\">Macmillan Cancertalk Week &amp; Cervical Cancer Awareness Week <span class=\\"morelink\\"><a href=\\"the-bookstall-cancer-links-and-resources.php\\">more...</a></span></h6>";
	}	
	// Macmillan Cancertalk week (21-25 Jan), Cervical Cancer Awareness Week (24-30 Jan) and Beating Bowel Cancer - Be Loud Be Clear Week (25-31 Jan)	
	else if ((date('m') == 01) && (date('d') == 25)) {
	echo "<img src=\\"images/ribbons/macmillan_cervical_bowel.gif\\" height=\\"145\\" width=\\"175\\" alt=\\"Macmillan Cancertalk, white & teal awareness ribbons, and blue & brown cancer awareness ribbons\\" /><br /><h6 class=\\"awareness\\">Macmillan Cancertalk Week, Cervical Cancer Awareness Week, and Be Loud Be Clear Week (Beating Bowel Cancer) <span class=\\"morelink\\"><a href=\\"the-bookstall-cancer-links-and-resources.php\\">more...</a></span></h6>";
	}
	// Beating Bowel Cancer - Be Loud Be Clear Week (25-31 Jan)	
	else if ((date('m') == 01) && (date('d') == 31)) {
	echo "<img src=\\"images/ribbons/brown_blue_ribbon.gif\\" height=\\"145\\" width=\\"175\\" alt=\\"blue and brown cancer awareness ribbons\\" /><br /><h6 class=\\"awareness\\">Be Loud Be Clear Week (Beating Bowel Cancer) <span class=\\"morelink\\"><a href=\\"the-bookstall-cancer-links-and-resources.php\\">more...</a></span></h6>";
	}	
	// International Childhood Cancer Day (15 Feb)
	else if ((date('m') == 02) && (date('d') == 15)) {
	echo "<img src=\\"images/ribbons/gold_ribbon.gif\\" height=\\"145\\" width=\\"175\\" alt=\\"gold cancer awareness ribbons\\" /><br /><h6 class=\\"awareness\\">International Childhood Cancer Day <span class=\\"morelink\\"><a href=\\"the-bookstall-cancer-links-and-resources.php\\">more...</a></span></h6>";
	}		
	// Gynaecological Cancers Campaign (1 Feb to 31 March)
	else if ((date('m') == 02) && (date('d') >= 01) || (date('m') == 02) && (date('d') <= 28)) {
	echo "<img src=\\"images/ribbons/teal_ribbon.gif\\" height=\\"145\\" width=\\"175\\" alt=\\"teal cancer awareness ribbons\\" /><br /><h6 class=\\"awareness\\">Gynaecological Cancers Campaign (1st February &ndash; 31st March) <span class=\\"morelink\\"><a href=\\"the-bookstall-cancer-links-and-resources.php\\">more...</a></span></h6>";
	}	
	else {
	echo "<a class=\\"awareness_link\\" href=\\"the-bookstall-cancer-links-and-resources.php\\"><img src=\\"images/ribbons/default_ribbon.gif\\" height=\\"145\\" width=\\"175\\" alt=\\"calendar\\" /><br /><h6 class=\\"awareness\\">Check our awareness calendar for information about awareness events &ndash; <span class=\\"morelink\\"><a href=\\"the-bookstall-cancer-links-and-resources.php\\">more...</a></span></h6></a>";
	}
?>

Calling date(‘m’) and date(‘d’) a hundred times will probably be very slow (PHP has to work out the date from an OS call). You should cache those values in a variable, then put the variable in the if-else.

You can actually try something like this, which I’ve seen before:

switch (true) {
  case ($month = 10 && $day >= 1 && $day <= 10):
    echo "<img src....>";
    break;
}//switch

But then, you might as well just use an if/else if/else block if you are doing that!