根据PHP中的日期显示图像 [英] Display image based on date in PHP

查看:108
本文介绍了根据PHP中的日期显示图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的电脑某处,我有一个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. <_<

也许我使用错误的搜索字词(我一直在尝试像php显示图像日期),但我没有发现任何类似的东西。

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?

我想我需要指定一个默认的图像,如果当前日期有一个特定的图像指定,可能一个case / break代码块可能是一个更好的方式来做,而不是/ else ....

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?

编辑:感谢大家的建议。我不是特别热衷于使用if / else / elseif,但最终它似乎是最简单的方式来完成它。我这样做的方式可能不是最有效的代码方式,但它现在适用。

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)

嗯...好的,谢谢。这就解释了为什么我想做的不是工作! :lol:

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

虽然我已经看到其他方法,包括foreach循环和GD,但我坚持使用if / elseif / else。这可能不是最有效的代码方式,但最终(代码的一部分 - 这是一个很长的列表,我不会让你所有的一切!):

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


推荐答案

可以使用 ()函数来检查当前月/日/年以及一些简单的if / else结构以显示不同的图像。

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

这篇关于根据PHP中的日期显示图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆