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!):

JavaScript

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