Here’s a simple, useful calendar script. This is a great way to check on days of the week for birthdays, holidays and other important occasions. You can also use it in conjunction with a data file (e.g. .csv, .xml) or database table (e.g. MySQL) as a tool to track appointments and special events.
This particular script makes many uses of the PHP date formatting functions. For more information on how PHP date formating works, see the PHP manual (http://us3.php.net/date).
First off, check to see if the user has sent previous values for the month and year via the form or the query string.
If this is the initial load of the form, then the system will pull the current month and year.
The “isset” function checks for the existence of the $_GET (query string) variables.
<!--//if values are not set for $Month and $Year, get current date values
if && (!isset($_GET["Year"]))) {
$Month = date("m");
$Year = date("Y");
}
else{
$Month = $_GET["Month"];
$Year = $_GET["Year"];
}
Now, we use the month and year values to get the Unix timestamp:
//Get viewed month
$Timestamp = mktime(0,0,0,$Month,1,$Year);
We can use the date formatting function to get the full month name (e.g. January, February, etc.):
$MonthName = date("F", $Timestamp);
We use the “echo” function to print the start of the table to the screen:
//Make calendar table
echo("Calendar Table for $MonthName, $Year
");
echo(
<table border="0" cellspacing="0" cellpadding="3" align="center">");
echo("
<tbody>
<tr bgcolor="#0000ff">");
We create the array for the days of the week and loop through the array elements to fill the table headers:
//loop through days
$daysOfWeek = array("Sun","Mon","Tue","Wed","Thu","Fri","Sat");
foreach ($daysOfWeek as $value) {
echo("
<td align="center"><strong>$value</strong></td>
");
}
echo("</tr>
");
Here is another date formatting function that we can use to get the day of the week for the first day of the month. Remember, this is a zero-indexed array. Thus, 0=Sunday, 1=Monday, etc.
//get day of week at start of month
$MonthStart = date("w", $Timestamp);
if ($MonthStart == 0) { // if the month starts on a Sunday
$MonthStart = 7;
}
Now we get the last day of the month by “0th” day of the next month:
//get last day of month
$LastDay = date("d", mktime(0,0,0,$Month+1, 0, $Year);
Another option is to find the number of days in the current month. Using this option will affect the calculations further down the routine. If you like, you can use this option and adapt the rest of the routine to this new setting.
//$LastDay = date("t", $Month);
Get the number of “blank” entries at the start of the month by using the day of the week that starts the month.
//get start date
$StartDate = -$MonthStart;
Now we can start filling the table cells for the calendar.
First, the we set up six rows for up to six weeks:
for ($k=1;$k<=6,$k++){ //print six rows for six possible weeks
echo("<tr>");
Then, we set up the columns for the seven days of the week:
for ($j=1;$j<=7;$j++){ //seven columns per row
The system will add 1 to the $StartDate variable for each day in the week. From there, we test the value of the variable. If the $StartDate variable is either before the start of the month or after the end of the month, the calendar shows a blank space. If the $StartDate variable is within the number of days of the month, the calendar prints the date.
$StartDate++;
if(($StartDate $LastDay) { //blank calendar space
echo("
<td> </td>
");
}
elseif { //date goes here
echo("
<td>$StartDate</td>
");
}
}
At the end of the week, we end the table row and start a new week.
echo("</tr>
");
} //End Table Row
After we’ve finished all six weeks, we end the table.
echo("</tbody></table>
");
That takes care of the current month. Now what do we do if we want to see another month?
Here’s how we create a form that allows a user to select a different month:
//create select form
echo("
<div><form action="\calendar.php\" accept-charset="UNKNOWN" enctype="application/x-www-form-urlencoded" method="\GET\">");
echo("Select a new month to view:
");
</form></div>
<form action="\calendar.php\" accept-charset="UNKNOWN" enctype="application/x-www-form-urlencoded" method="\GET\">
Instead of listing the months individually, we can use the date formatting function to create a dynamic loop and display the month names:
echo(""); for($m=1;$m<=12;$m++){ echo("" .date("F", mktime(0,0,0,$m,1,$Year)) . ""); } echo("");
We can do the same for the year (2008-2020):
echo(""); for($y=2008;$y<2020;$y++){ echo("$y"); } echo("");
Now we finish the form with the submit button:
echo("");
echo("");
</form>
Again, this is a simple yet functional calendar script. If you’d like to see how to add other features to this calendar, such as flat file or database support, please let us know.