views:

463

answers:

5

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

+4  A: 

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

ZeissS
Thanks for your suggestion. If/else wasn't my preferred way of doing it initially, but in the end seemed the simplest way to accomplish it. Probably the way I've done it isn't the most efficient code-wise, but it works for now. Thanks again.
NeonBlue Bliss
+1  A: 

The best way to do it would probably be just to use GD to display it.

<?php

// Create a 75*15 image
$im = imagecreate(75, 15);

// White background and black text
$bg = imagecolorallocate($im, 255, 255, 255);
$textcolor = imagecolorallocate($im, 0, 0, 0);

// Write the date at the top left, offset by 2px to the right
imagestring($im, 5, 2, 0, date("m/d/y"), $textcolor);

// Output the image
header('Content-type: image/png');

imagepng($im);
imagedestroy($im);

?>
Xorlev
Thanks - that's really useful. I've never really looked into GD and what it can do, but that's a nice example
NeonBlue Bliss
You can also use actual fonts...just upload arial.ttf and such. ImageMagick can do even more advanced compositing.
Xorlev
+1  A: 
switch(date('Y-m-d')) {
  // multiple dates with same image
  case '2010-02-15':
  case '2010-02-07':
    print '<img src="/path/to/image.jpg" alt="" />';
    break;
  // really long ranges don't work all that well in this solution
  case '2010-03-01':
  case '2010-03-02':
  case '2010-03-03':
  case '2010-03-04':
  case '2010-03-05':
  case '2010-03-06':
  case '2010-03-07':
  case '2010-03-08':
  case '2010-03-09':
  case '2010-03-10':
    print '<img src="/path/to/image.jpg" alt="" />';
    break;
  // specific date image
  case '2010-12-25':
    print '<img src="/path/to/christmas.jpg" alt="" />';
    break;
  // fallback image
  default:
    print '<img src="/path/to/default.jpg" alt="" />';
    break;
}

If you're doing a lot of largeish date ranges, this'll break down, but it works well for a few smallish ranges and specific dates.

ceejayoz
Thanks - that looks very similar to what I remember of the script I had. The date ranges will be a month or maybe two months on odd occasions at most - the rest will either be weeks or days. There's just a couple of potential problems I'm wondering about. The first is whether it'd worth without the year (i.e. with just a month and day), and also what would happen if there was a day say with an image that was within a date range that was specified with a different image. I want to use it to display awareness ribbons for different cancers, and there may also be a day within a date range.
NeonBlue Bliss
Yep, you could skip the year entirely if that suits your usage case.In the case of a date matching multiple conditions, the first one specified will be the one that shows up (due to the `break` statement). Put the most specific dates first and the more general ranges towards the bottom and you'll be in good shape.
ceejayoz
NeonBlue Bliss
Really long ranges don't work that well in this solution - it's more designed for short ranges or individual dates. Just updated with an example. If you're dealing with a lot of ranges, you may be better off with a database to store your criteria - they handle selecting this sort of data much better.
ceejayoz
+1  A: 

It sounds like you were copy/pasting your code before, but this is really a good snippet to learn on if you're up for it. The basic construct you're looking for is:

$today = getdate();
$day = $today['wday'];
if ($day == 1 OR $day == 2 OR $day == 3){
    echo "<img src='whatever image you want'>";
}
elseif ($day == 4 OR $day ==5){
    echo "<img src='another image' />";
}
else { echo "<img src='default image' />";}

Basically you find the date, and using IF/ELSE loops, determine if today is in one range or another. IF it is, echo out the image you want to show.

In this example, the variable $day is set to a number 1-7. 1==Monday, 2==Tuesday, 3==Wednesday... If it's Monday Tuesday or Wednesday, it shows one image, (the if ($day == 1 OR $day == 2 OR $day == 3) line), Thursday/Friday shows another, and otherwise it shows the "default" image, which shows on the weekend. Obviously this isn't the EXACT case of days/dates you want, but something like this construct will help you out.

Check out the PHP Date reference for help with picking just what dates you want to compare.

Alex Mcp
Thanks Alex - that's a really useful snippet to know.
NeonBlue Bliss
+1  A: 

This is my solution, I guest maybe you do not care about which year, so this solution just consider month and date:

function image_of_date($default, $options) {
    $today = date('md');
    foreach ($options as $item) {
        $src = $item[0];
        $begin = $item[1];
        $end = (3===count($item))? $item[2] : $begin;
        $begin = date('md', strtotime($begin));
        $end = date('md', strtotime($end));
        if ($today >= $begin && $today <= $end) {
            return $src;
        }
    }
    return $default;
}


echo image_of_date('default.png', array(
    array('jan-01.png', 'Jan 1'),
    array('feb.png', 'Feb 1', 'Feb 29')
));
XUE Can
Thanks Xue - that solves the problem of just using the month and day, so I can just specify the month and day though. As in my comment below though, I'm wondering what would happen if there was a day say with an image that was within a date range that was specified with a different image. I want to use it to display awareness ribbons for different cancers, and there may also be a day within a date range - i.e. there may be more than one image for a particular date
NeonBlue Bliss