Page 1 of 1
Change skin depending on date
Posted: Thu Nov 18, 2004 6:03 pm
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
Posted: Thu Nov 18, 2004 6:10 pm
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
-
Posted: Thu Nov 18, 2004 7:52 pm
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
Posted: Fri Nov 19, 2004 2:38 am
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
-
Posted: Fri Nov 19, 2004 3:08 am
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'
Posted: Fri Nov 19, 2004 4:13 am
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')];
?>
Posted: Fri Nov 19, 2004 5:44 am
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";
Posted: Fri Nov 19, 2004 12:01 pm
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.