How to change stylesheets monthly using PHP

I wonder if my site change along the season…? Yes, it has the ability to do so. Since my server supports PHP, I can tell PHP to do that job. For example, when it’s my birthday, PHP changes the default 'style.css' to something like 'mybirthday.css' changing my whole website colors to something else, on behalf of me. Even PHP can change my cascade style sheet daily, monthly or yearly. Don’t ask me how to, just see below. If you are new to PHP, I will help you figure this tip out. If you are PHP-Guru, may be, I reminded you something you never thought of.

This tip is divided into two parts. (1) The manual part. (2) The smarter part.

1. The manual part:

This part is simple but too longer than PART 2 script, however it’s simpler to understand without further description. In this part date() function is working with the "m" (in small letter) parameter where "m" is the months of the year in numeric format in which zero is the prefix (01, 02 – 12). For further parameters of day, week, month and year official PHP date() manual is waitng you here.

Part 1 PHP Code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<?php
//  What month is it?
$thisMonth = date(m);
 
// If month is January, call January CSS
if($thisMonth ==01){
$myStyle = "january.css";
}
 
// If month is February, call Fabruary CSS
elseif($thisMonth==02){
$myStyle = "february.css";
}
 
// If month is March, call March CSS
elseif($thisMonth==03){
$myStyle = "march.css";
}
 
// If month is April, call April CSS
elseif($thisMonth==04){
$myStyle = "april.css";
}
 
// If month is May, call May CSS
elseif($thisMonth==05){
$myStyle = "may.css";
}
 
// If month is June, call June CSS
elseif($thisMonth==06){
$myStyle = "june.css";
}
 
// If month is July, call July CSS
elseif($thisMonth==07){
$myStyle = "july.css";
}
 
// If month is August, call August CSS
elseif($thisMonth==08){
$myStyle = "august.css";
}
 
// If month is September, call September CSS
elseif($thisMonth==09){
$myStyle = "september.css";
}
 
// If month is October, call October CSS
elseif($thisMonth==10){
$myStyle = "october.css";
}
 
// If month is November, call November CSS
elseif($thisMonth==11){
$myStyle = "november.css";
}
 
// If month is December, call December CSS
elseif($thisMonth==12){
$myStyle = "december.css";
}
?>

Description:
It’s obvious what I was meant for the above, since its simple enough, you’re almost on there. Let me say few words after it.

Code:

1
2
3
4
 
$thisMonth = date(m); 
 
// The rest

$thisMonth variable finds what month it is using date() function “m” is a Numeric parameter representing the MONTH, from 1 up to 12 with leading zeros. i.e. (01, 02, 03 … 12).

Code:

1
2
3
4
5
6
7
8
 
// Some code  
 
if($thisMonth ==01){ 
$myStyle = "january.css"; 
} 
 
// The rest of the code

Using the IF argument $thisMonth variable compares the current month, then $myStyle variable searches a previously specified CSS file which has the current month’s name, and then places in its value.

The rest of the code is the same unless we are using ELSEIF until the end.

To Do:

1. If your template has .html extension change it to .php i.e. index.php
2. Copy and paste the above code before the html tag of your template.
3. Remove your default style sheet link from the section of your template.
4. Copy and Paste the code below in the head section of your template.

Code:

1
<link rel="stylesheet" type="text/css" href="<?php echo $myStyle;?>" media="screen" />

2. The smarter part:

I call this part “Smarter” since its too shorter than PART 1. In this part date() function uses "F" (in upper case) parameter, where "F" is A full textual representation of a month, i.e. January, March, December, etc…

Part 2 Script:

1
2
3
<?php
$thisMonth = date(F);
?>

Oops! Too short, hehh?

Desription:

$thisMonth variable calls the name of the CURRENT month in a full textual representation. Since we have 12 CSS files, $thisMonth variable searches the exact file that has the properties of the current month and places in the $thisMonth value. No matter for capitalization, PHP will understand what did you mean…

To Do:

1. If your template has .html extension change it to .php i.e. index.php
2. Copy and paste the above code before html the tag of your template.
3. Remove your default style sheet link from the section of your template.
4. Paste the following code in the head section of your template.

Code:

1
<link rel="stylesheet" href="<?php echo $thisMonth; echo "."; echo "css";?>" type="text/css" media="screen">

I think you understood what the three ECHO’s represent. The first ECHO calls $thisMonth value, the second ECHO creates the "." separator and the last ECHO brings the "css" extension, all in all many hands made success.

Shortcut:

If you are curious to taste this script, just make 12 copies of your default CSS file then rename each (call each a name of a month). Change your computer’s date and refresh the page. View the source of the page. Check the name of your CSS file. It must be something like this code:

1
<link rel="stylesheet" type="text/css" href="february.css" media="screen" />

Or as part will show:

1
<link rel="stylesheet" type="text/css" href="February.css" media="screen" />

February.css, because PHP capitalizes the first character of the month’s name…

;) Take care…

Popularity: 19% [?]

About the Author

J Mehmett

Well, I’m J Mehmett. The owner and maintainer of this tinny blog. I'm Photoshop addicted, PHP lover, CSS admirer, WordPress fan and Mr. Internet wanna be. I’m a person who really loves the computers much that I give them every spare time I do have. For More about me, please hit "about" menu in the top navigation panel. Thank you.

One Response to “ How to change stylesheets monthly using PHP ”

  1. Nice tips, It’s pretty tricky…

Leave a Reply

You can use these XHTML tags: <a href="" title=""> <abbr title=""> <acronym title=""> <blockquote cite=""> <code> <em> <strong>