Log in

View Full Version : Display an Image depending for only a specific date range.



Relics
11-10-2009, 05:59 PM
I'm currently working on a project and slowly learning PHP and Wordpress. I've got the following problem that if anyone can help me with by providing me the code to learn from, I would greatly appreciate it.

I need a specific image ("purchase_tickets_img.png") to only appear during a specific range of dates (September 20th - November 5th) using PHP. If it's not those dates, then nothing should be displayed. I need it to be a hyperlinked image, and I've got a javascript providing a roll-over effect for the image.


<img src="/images/purchasetickets_img.png" srcover="/images/purchasetickets_img_active.png" class="alignright" />

Could someone be so kind as to explain / show me how the proper code to create this month based PHP scripted image that still allows it to be linked and have the roll-over effect.

Schmoopy
11-10-2009, 09:21 PM
<?php
date_default_timezone_set('Europe/London'); // Change this depending on what timezone your in

$today = strtotime(date('Y-m-d'));
$start = strtotime(date('Y') . '-09-20');
$end = strtotime(date('Y') . '-11-05');

if($today >= $start && $today <= $end) { ?>
<img src="/images/purchasetickets_img.png" srcover="/images/purchasetickets_img_active.png" class="alignright" />
<?php
}
?>


Should do the trick. Don't know if it's the most efficient way to do it, but it works.

Displays the image if today's date meets the conditions, otherwise shows nothing. Any link you want to add to the image, or JavaScript, can be put in between the two curly braces.