Feb
27
2005
I was recently faced with the daunting task of creating a calendar using PHP and honestly I had no clue where to start. I looked online for various tutorials and walkthroughs but found none suitable to my needs so I did what any self respecting Developer does in this position. I freaked out. After a good freak out session I pulled myself together and just took bits and pieces of code that I knew I needed and started from there. The following is a “for Dummies” explanation of how I got a calendar working and some options available to you for expansion and modification. I’ve included links to the PHP reference manual from functions that may not be so common.
I built a separate function for my calendar because I was sure I was going to need it in more than one place, I named it buildcalendar and put it into an include file as includes/calendar.inc.php. In order to show the current month, the function needs todays day, month and year so it ended up reading like this…
function buildcalendar($today,$month,$year)
{
}
So before I call this function I need to set the day, month and year to $today,$month and $year respectively. This can easily be acheived using the getdate() function as shown below in my main file calendar.php…
$date = getdate();
$today = $date[’mday’];
$month = $date[’mon’];
$year = $date[’year’];
The mday, mon and year pieces are calling to the array that getdate() sets.
Ok, so now we have our function and the variables it needs to work but what goes into the function? I will first provide the code then explain it…
function buildcalendar($today,$month,$year) {
$daysOfWeek = array(’Su’,'Mo’,'Tu’,'We’,'Th’,'Fr’,'Sa’);
$firstDayOfMonth = mktime(0,0,0,$month,1,$year);
$numberDays = date(’t',$firstDayOfMonth);
$dateComponents = getdate($firstDayOfMonth);
$monthName = $dateComponents[’month’];
$dayOfWeek = $dateComponents[’wday’];
$calendar = "<H6>$monthName, $year </H6>";
$calendar .= "<table class=’calendar’>";
$calendar .= "<tr>";
foreach($daysOfWeek as $day) {
$calendar .= "<th class=’header’>$day</th>";
}
$currentDay = 1;
$calendar .= "</tr><tr>";
if ($dayOfWeek > 0) {
$calendar .= "<td colspan=’$dayOfWeek’> </td>";
}
while ($currentDay <= $numberDays) {
if ($dayOfWeek == 7) {
$dayOfWeek = 0;
$calendar .= "</tr><tr>";
}
$calendar .= "<td class=’day’>$currentDay</td>n";
$currentDay++;
$dayOfWeek++;
}
if ($dayOfWeek != 7) {
$remainingDays = 7 - $dayOfWeek;
$calendar .= "<td colspan=’$remainingDays’> </td>";
}
$calendar .= "</table>";
return $calendar;
}
On the first line I created an array with the day abbreviations, next I wanted to find the first day of the current month using the mktime function using the values I set within calendar.php.
From there I needed to get the total number of days in the current month, for this I used the date function.
In order to find out more about the first day of the month, I called the getdate function again as I did before. In the next 2 lines I set the month name and the day of the week that this first day fell on.
This first $calendar sets the header text which is the month and year. The next two set up the table that the calendar will be displayed in.
I then used a foreach function to run through the weekday names I set before and put them in the first row of cells.
Here’s where the real fun begins! I started a counter $currentDay and set it to 1, then I started a new table row in $calendar and set a colspan in the first <td> tag set to $dayOfWeek so that the calendar displays the days evenly.
Next is a fairly large while loop that first finds if the day is the last (if ($dayOfWeek == 7)) and if it is it will start a new row. If not I set another day in $calendar. Finally, I incremented the $dayOfWeek and $currentDay for the next go around.
When all the days in the month are set, I then check to see if the last day is still in the middle of the week(if ($dayOfWeek != 7)) and if so made a final cell that spanned the remaining days ($remainingDays = 7 - $dayOfWeek;).
To finish it off I closed the table and set the return $calendar; to return the calendar table.
Finally, in calendar.php after the code to find the current day put in your include to the function file and a call to the function itself. Your final file shoul look like this…
<html>
<head>
<link rel=”stylesheet” type=”text/css” href=”includes/calendar.css” />
</head>
<body>
<?php
$date = getdate();<br />
$today = $date[’mday’];<br />
$month = $date[’mon’];<br />
$year = $date[’year’];<br /><br />
include “includes/calendar.inc.php”;<br />
echo "<div align=’center’>". buildcalendar($today,$month,$year,$dateArray)." </div>";<br />
?>
</body>
</html>
Following is the CSS I made to correspond to the calendar, I called it includes/calendar.css…
h6 {
font-family:arial,helvetica;
font-size:11px;
color: black;
font-weight: bold;
text-align:center;
}
th {
background-color:#ccc;
}
.calendar {
font-family:arial,helvetica;
font-size:11px;
color: white;
background-color: #dedede;
border-color: #000000;
border-style: solid;
border-width: 1px;
}
.day {
background-color: #808080;
border-color: #000000;
border-style: solid;
border-width: 1px;
text-align: center;
}
.header {
background-color: #ccc;
border-color: #000000;
border-style: solid;
border-width: 1px;
color:#000000;
}




All Content Copyright ©2008 SleepingAwake.net.