Change skin depending on date

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
cg_rocker
Forum Newbie
Posts: 3
Joined: Thu Nov 18, 2004 5:46 pm

Change skin depending on date

Post by cg_rocker »

I'm redesigning my site, and creating three sets of "skins". Depending on what the current system date is, I want to load in the graphics for either summer, fall, or winter.

The graphics files will be named something like this:

summer_pic1.jpg
summer_pic2.jpg
summer_pic3.jpg...

and

fall_pic1.jpg
fall_pic2.jpg
fall_pic3.jpg, etc.

I want PHP to grab the date off the server, and assign a variable to $season (summer, fall, or winter). I'll then use $season as the prefix to my graphics files throughout my pages.

Any suggestions as to the most efficient way to accomplish this?

Thanks,
Chris
djot
Forum Contributor
Posts: 313
Joined: Wed Jan 14, 2004 10:21 am
Location: planet earth
Contact:

Post by djot »

-
Hi,

Not the best one perhaps, but perhaps a start.

You may get the timestamp for january, 1st. with mktime(), then add the days for whatever intervall to find out which season currently is the right one. The timestamp returned will have to be increased by days*hours*minutes*seconds.

Even simplier: Just extract the no. of the current month with date() and choose summer e.g. for month 6-9 or whatever you prefer.

djot
-
cg_rocker
Forum Newbie
Posts: 3
Joined: Thu Nov 18, 2004 5:46 pm

Post by cg_rocker »

Thanks for the reply, Djot.

I think I've got a handle on how to retrieve the date. What I'm really trying to find, is the best way to set my variable for $season.

Should I set a cookie? Write the variable to a database field and query it for each page? Start a session that stores the variable for me?

I realize there are several ways to accomplish this. I'm curious how the experts would handle it.

Thanks again,
Chris
djot
Forum Contributor
Posts: 313
Joined: Wed Jan 14, 2004 10:21 am
Location: planet earth
Contact:

Post by djot »

-
Hi,

Cookies or sessions? These are normally used for storing user/client data, or working data/steps like in forms or wizards/dialogs.

Anyone who would store the $season as cookie/session? Not me.

Normally I have a global variable for things like that, that is included via config.php etc. (exactly I have a special datetime.php config-file that handles all date/time related stuff)

About the database: You would have to check for the right season each time, update the database and retrieve back the season. And then? Again the same question on how to store the data; cookie? session? constant? -> no gain here. A lot of overhead I guess, for just getting nearly nondynamic single data. A simple global $season = date(...); would just do the job.

Sorry for not being an expert :)

djot
-
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

Post by phpScott »

For ease of use I would store it in a session but another alternative would be to have a config file that uses a case statement to determine which config file to include that has the images, and anything else that you want to include in your seasonal 'skin'
rehfeld
Forum Regular
Posts: 741
Joined: Mon Oct 18, 2004 8:14 pm

Post by rehfeld »

i wouldnt bother storing this in a session/cookie or db, this prob only takes a few microseconds to execute.... a databse query would surely take longer than to just have php parse this

Code: Select all

<?php

$month = date('n');

switch($month) {
    case 1:
        $image = 'winter.jpg';
        break;
    case 2:
        $image = 'winter.jpg';
        break;
    case 3:
        $image = 'winter.jpg';
        break;
    case 4:
        $image = 'spring.jpg';
        break;
} // etc....


// or just use an array, prob even faster

$seasonal_images = array(
    1 => 'winter.jpg',
    2 => 'winter.jpg',
    3 => 'winter.jpg',
    4 => 'spring.jpg',
);

$bg_image = $seasonal_images[date('n')];


?>
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

here's is what I would use:

Code: Select all

function season_by_date($date) {
    return season_by_month( date('n', $date) );
}

function season_by_month($month) {
    return (int)($month / 3) % 4;
}
$seasons = array('winter', 'spring', 'summer', 'fall');
echo "It is " . $seasons[season_by_date(time())] . " now";
cg_rocker
Forum Newbie
Posts: 3
Joined: Thu Nov 18, 2004 5:46 pm

Post by cg_rocker »

Okay you've talked me out of the db/session/cookie approach. I'll use some of the above code and include it in my header file for each page.

Thanks for all the help, gang.
Post Reply