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开发者_C百科 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 & 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 – 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 – <span class=\"morelink\"><a href=\"the-bookstall-cancer-links-and-resources.php\">more...</a></span></h6></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.
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);
?>
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.
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')
));
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.
Have you considered using an array that you loop through?
$events = array(
array(
'image' => 'img/1.png',
'start' => '01-02-2014',
'end' => '02-02-2014'
),
array(
'image' => 'img/2.png',
'start' => '03-02-2014',
'end' => '04-02-2014'
)
);
and then you simply continue it on with
foreach($events as $event) {
if(date('d-m-Y') >= $event['start'] && date('d-m-Y') <= $event['end']) {
echo "<img src='".$event['image']."'>";
}
}
Hopefully this is a little simpler/cleaner for you. And of course you can add in any additional details you want to the array.
~James.
Be careful using two digit dates that contain a leading zero. PHP interprets this as an octal numeral and it will return an error if '08' and '09' are used. I found this out the hard way when trying to set September as a month for my elseif case.
I think there is a much better, more sophisticated solution to get a different image for each day of the year to come.
The belowmentioned script will show you every day a different image. The available images will be rotated and not randomly selected. The script will work with any number of images available, so you don't have to upload 365/366 images.
Preparation:
- create a unique directory dedicated for your images.
- give all the images names linke 0.jpg // 1.jpg // 2.jpg // 3.jpg // and so on.
- and integrate the belowmentioned php-script at the place where you want in to show up.
- that's all.
精彩评论