Skip to content
Advertisement

Display image based on date in PHP

Somewhere on my computer I had a PHP script for displaying an image based on the date, that would allow me 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.

I recently had a problem with one of my hard drives though 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 found the script though. I’ve looked all over online and can’t find it again. I thought it was here, but after searching around I can’t find anything vaguely like it, let alone the script itself unfortunately. <_<

Maybe I’m using the wrong search terms (I’ve been trying things like “php display image date”), but I’m finding nothing similar.

Does anyone know of anything fitting the description above, or can suggest the best way to do this?

I’m thinking that I need to specify a default image for if the current date’s got a specific image specified and probably a case/break code block might be a better way to do it than if/else….

Anyone got any thoughts on the best way to do this?

Edit: Thanks everyone for your suggestions. I wasn’t especially keen on using if/else/elseif, but in the end it seemed the easiest way to accomplish it. The way I’ve done it’s probably not the most efficient way code-wise, but it works for now.

(part of the code – it’s rather long, so I won’t bore you with it all)

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

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

Advertisement

Answer

you could use the date() function to check for the current month/day/year and some simple if/else constructs to show different images.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement