Once upon a time, my friends and I decided to start a group weblog. Our group used to be small enough that we'd always remember each other's birthdays, but we now have 24 people with posting privileges, and as a result, someone always slips through the cracks.
Most people like having their birthdays remembered, so I thought I'd bash out something quick and dirty to test the current date against everyone's birthdays. If there was a match, a little text message would be displayed at the top of the page.
I did some thinking, and a situation like this (with only two required variables) is a nice way to use a switch…case statement.
When I say that this script is nothing but a big switch…case statement, I wasn't kidding. In PHP, switch($variable) tests for the value of $variable. In this case…
switch ($month) {
case 1:
default: break;
case 2:
default: break;
case 3:
default: break;
}
etc. All it's saying is, if it's a certain month, check this particular block of text.
Obviously, you're going to want to display a birthday on one day, not the entire month. How do we do this? If $month matches, then we…you guessed it…run another switch statement to test the $day variable.
Since you only want to parse one month/day combination, you need to force the code to break at the end of each case. Otherwise, you can get odd results.
Since I want to execute a particular block of code if the month/day combination match one of the ones on file, that's a great use for a function. I stash these lines up at the top of the page, so that the function is defined by the time you get to the switch…case statement:
<?
function display_user_birthday($birthday_geek) {
echo "<div class=\"birthday\">Happy birthday to " . $birthday_geek . "!</div>";
}
?>
Place this section of PHP in your page where you want the actual birthday reminder to be shown:
<?
# My server runs on GMT, but most of the geek-chick.net members are Stateside,
# so I'm arbitrarily subtracting five hours from the current time.
# Doesn't make a big difference either way, but otherwise birthday
# notifications would start at 7 p.m. the day before, and end at 7 p.m.
# the day *of*. If your server time is the time you want to use,
# just delete the -5.
$today = getdate(mktime(date("H")-5));
# getdate() returns an array. We want the numbers for the month
# and the day of the month
$month = $today['mon'];
$day = $today['mday'];
# If you want to test the script to make absolutely certain it's working,
# uncomment these next two lines and change them to a date that you've set
# below:
# $month = 6;
# $day = 1;
# If you don't need a particular month, delete everything from the word 'case'
# to the next default: break; line.
switch ($month) {
# if it's January...
case 1:
# check these dates...
switch ($day) {
case 1: display_user_birthday(""); break;
case 2: display_user_birthday(""); break;
}
default: break;
# if it's February...
case 2:
# check these dates...
switch ($day) {
case 1: display_user_birthday(""); break;
}
default: break;
# if it's March...
case 3:
# check these dates...
switch ($day) {
case 1: display_user_birthday(""); break;
}
default: break;
# if it's April...
case 4:
# check these dates...
switch ($day) {
case 1: display_user_birthday(""); break;
}
default: break;
# if it's May...
case 5:
# check these dates...
switch ($day) {
case 1: display_user_birthday(""); break; }
default: break;
# if it's June...
case 6:
# check these dates...
switch ($day) {
case 1: display_user_birthday(""); break;
}
default: break;
# if it's July...
case 7:
# check these dates...
switch ($day) {
case 1: display_user_birthday(""); break;
}
default: break;
# if it's August...
case 8:
# check these dates...
switch ($day) {
case 1: display_user_birthday(""); break;
}
default: break;
# if it's September...
case 9:
# check these dates...
switch ($day) {
case 1: display_user_birthday(""); break;
}
default: break;
# if it's October...
case 10:
# check these dates...
switch ($day) {
case 16: display_user_birthday("Jeff"); break;
case 20: display_user_birthday("Amy"); break;
}
default: break;
# if it's November...
case 11:
# check these dates...
switch ($day) {
case 1: display_user_birthday(""); break;
}
default: break;
# if it's December...
case 12:
# check these dates...
switch ($day) {
case 1: display_user_birthday(""); break;
}
default: break;
?>
Take a look at the month of October. Since my birthday is 10/20, a match will be returned for me if $month = 10 and $day = 20. If a match is found, the following function is executed:
display_user_birthday("Amy");
and execution of the script is broken (meaning no more dates will be checked).
If you'll take a look at the function for display_user_birthday(), you'll see that it takes whatever you put inside the (" ") marks as its 'argument.' In other word, whatever is between (" ") becomes the value of $birthday_geek. This function will output this line:
<div class="birthday">Happy birthday to Amy!</div>
The end result: nobody's birthday is forgotten, everyone's happy, and I'm not checking my datebook at the beginning of each month to figure out if there are any birthdays I need to remember this month.
This script is implemented at geek-chick.net, but currently there's nothing to see. It will generate output on June 14, 24, and 25 (for Chris, Rob, and Alice respectively) if you want to keep an eye on it, though.