tags:

views:

26

answers:

2

Hi Folks,

I just wondered if anybody can point me in the right direction: I'm looking to make a script whereby the logo on my site changes depending on the date; so for instance a haloween style one soon.

I started off by having 2 arrays, 1 of start dates and 1 of end dates(not sure even if this is the best way!):

<?php
$start_dates = array('01/01' => 'New Years',
                     '14/02' => 'Valentine Day',
                     '16/02/2010' => 'Pancake Day',
                     '17/03' => 'St Patricks Day',
                     '01/04' => 'April Fools',
         '02/04/2010' => 'Easter',
         '23/04' => 'St Georges Day',
         '11/06/2010' => 'World Cup',
         '31/10' => 'Halloween',
         '05/11' => 'Guy Fawkes',
         '11/11' => 'Armistice Day',
         '16/10' => 'Today',
         '15/12' => 'Christmas');

$end_dates = array( '08/01' => 'New Years',
                    '15/02' => 'Valentine Day',
                    '17/02/2010' => 'Pancake Day',
                    '18/03' => 'St Patricks Day',
        '02/04' => 'April Fools',
                    '06/04/2010' => 'Easter',
        '24/04' => 'St Georges Day',
        '12/07/2010' => 'World Cup',
        '01/11' => 'Halloween',
        '06/11' => 'Guy Fawkes',
        '12/11' => 'Armistice Day',
        '17/10' => 'Today',
        '01/01' => 'Christmas');
?>

Easy so far...the problemis that I need a way of working out if todays date falls between the start date and end date, then changing the image file name.

Its a long shot but I hope someone would be kind enough to help.

Thanks, B.

A: 

like this

$events = array(
 'New Year' => '01/01 01/08',
 'Pancake Day' => '16/02/2010 17/02/2010',
 //etc
);

echo find_event($events, '16/02');

where find_event() is

function mdy2time($date) {
 $e = explode('/', $date);
 if(count($e) < 3)
  $e[] = '2010';
 return strtotime("$e[1]-$e[0]-$e[2]");
}

function find_event($events, $date = null) {
 $date = is_null($date) ? time() : mdy2time($date);

 foreach($events as $name => $range) {
  list($start, $end) = explode(' ', $range);
  if($date >= mdy2time($start) && $date <= mdy2time($end))
   return $name;
 }
 return null;
}
stereofrog
A: 

you should use an array more like this:

$dates = array();
$dates[] = array(
    'name' => 'New Years'
    'start' = '01/14',
    'end' => '01/20',
    'style' => 'haloween',
);
$dates[] = array(
    //...
);

then you can get the style as follows:

$style='default';

// date as number e.g. 130 (january 30th)
$currDate = date('md',time()) * 1;
foreach ($dates AS $k => $v) {
    $tmp = explode("/",$v['start'];
    $start = ($tmp[1].$tmp[0])*1;
    $tmp = explode("/",$v['end'];
    $stop = ($tmp[1].$tmp[0])*1;
    if ($start <= $currDate && $currDate < $stop) {
        $style=$v['style'];
        break;
    }
}

echo 'style: '.$style;

Didn't check the code yet, so feel free to correct me if iam wrong.

justastefan