Create a Simple Calendar Using PHP
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.
Now, we use the month and year values to get the Unix timestamp:
We can use the date formatting function to get the full month name (e.g. January, February, etc.):
We use the "echo" function to print the start of the table to the screen:
We create the array for the days of the week and loop through the array elements to fill the table headers:
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.
$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:
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.
Get the number of "blank" entries at the start of the month by using the day of the week that starts the month.
$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:
Then, we set up the columns for the seven days of the week:
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.
At the end of the week, we end the table row and start a new week.
After we've finished all six weeks, we end the 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:
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:
We can do the same for the year (2008-2020):
Now we finish the form with the submit button:
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.


Want an avatar? Get a gravatar! • You can link to this comment
First let me say that I am new to PHP. I cannot get past line two without an error!
1<?php
2//if values are not set for $Month and $Year, get current date values
3if ((!isset($_GET["Month"]))&& (!isset($_GET["Year"])))
4 {
5 $Month = date(“m”);
6 $Year = date(“Y”);
7 }
8 else{
9 $Month = $_GET["Month"];
10 $Year = $_GET["Year"];
11 }
This is the error message
Parse error: syntax error, unexpected ‘;’ in C:\wamp\www\LAFestUpload\calendarTest.php on line 3
Any help would be appreciated.
Thank you.
Michael
Want an avatar? Get a gravatar! • You can link to this comment
thanks a lot for providing such a helpful script on the net.
……………….Thanks once again…………….
Want an avatar? Get a gravatar! • You can link to this comment
Neat idea nothing to see…
Demo, Preview or anything to see what it looks like?
Regards,
Cheers
Want an avatar? Get a gravatar! • You can link to this comment
Great code… but alot of mistakes,
im going to try and upload a revised version.
<?php
echo “”;
echo “Calendar Table for $MonthName, $Year”;
echo “”;
$daysOfWeek = array(“Sun”,”Mon”,”Tue”,”Wed”,”Thu”,”Fri”,”Sat”);
foreach ($daysOfWeek as $value) {
echo “$value“;
}
echo “”;
$MonthStart = date(“w”, $Timestamp);
if ($MonthStart == 0) {
// if the month starts on a Sunday
$MonthStart = 7;
}
$LastDay = date(“d”, mktime(0,0,0,$Month+1, 0, $Year));
$StartDate = -$MonthStart;
for ($k=1;$k<=6;$k++){ //print six rows for six possible weeks
echo”";
for ($j=1;$j<=7;$j++){ //seven columns per row
$StartDate++;
if($StartDate 0) {
echo”$StartDate \n”;
} else {
echo” \n”;
}
} elseif (($StartDate = $LastDay)) { //date goes here
if($StartDate >= 0) {
echo”$StartDate \n”;
}
}
}
echo”";
} //End Table Row
echo “”;
?>
<?php
echo “”;
for($m=1;$m<=12;$m++){
$selected = “”;
$longDate = date(“F”, mktime(0,0,0,$m,1,$Year));
if ($Month==$m){
$selected = “selected “;
}
echo “$longDate \n”;
}
echo “”;
echo “”;
for($y=2007;$y<=2020;$y++){
$selected = “”;
$longDate = date(“Y”, mktime(0,0,0,1,1,$y));
if ($Year==$y){
$selected = “selected \n”;
}
echo “$longDate \n”;
}
echo “”;
?>
Want an avatar? Get a gravatar! • You can link to this comment
WOW, that got eaten alive.. so much for trying.
for a working version go here:
http://www.dyertech.ca/current/cal.php
Want an avatar? Get a gravatar! • You can link to this comment
Nice article,
I had been pondering this but you saved me the brain power. A special thanks to KC for the example and working code.
Nice work
Want an avatar? Get a gravatar! • You can link to this comment
Nice article…
But there is an issue… in each month it shows as 1 day less than total number of days.
Want an avatar? Get a gravatar! • You can link to this comment
i have changed the following line,
$LastDay = date(“d”, mktime(0,0,0,$Month+1, 0, $Year));
into this,
$LastDay = date(“d”, mktime(0,0,0,$Month+1, 0, $Year))+1;
now it works fine
Want an avatar? Get a gravatar! • You can link to this comment
PHP is very new to me. Recently, my teacher give me a home work “build a calender using internet programming language”. I believe that this sample will be very useful to my work.
Want an avatar? Get a gravatar! • You can link to this comment
hi , that s great calendar! but can we change the days number into my own value.
maybe someone colud give some example…-php newbie