Publishing System Settings Logout Login Register
How To Create A Simple Calendar
TutorialCommentsThe AuthorReport Tutorial
Tutorial Avatar
Add to Favorites
Posted on December 28th, 2010
PHP Coding

  This tutorial is going to walk the user through creating a simple PHP calendar. This tutorial also helps the user create their own functions, and is an excellent introduction to the date() and calendar functions within PHP. Any novice/beginner programmer should be able to follow along.

  To start, we're going to create the function file. This function file will contain a few simple functions that will aid in the creation of the calendar. I have named mine functions.php, but you may name yours what ever you want.



function getDays() { // this function will return the number of days in the current month
  $mon = date('n'); // returns the month in number form w/o leading 0's. example: December would be 12, January would be 1.
  $yr = date('Y'); // returns the year as XXXX. example: 2010
  return cal_days_in_month(CAL_GREGORIAN, $mon, $yr); // We use the Gregorian calander.

function startDay() {
 $mon = date('n');
 $year = date('Y');
 $start = array(1 => "Sunday", 2 => "Monday", 3 => "Tuesday", 4 => "Wednesday", 5 => "Thursday", 6 => "Friday", 7 => "Saturday"); // this array matches the day of the week with the corresponding day number.
 $dow = date('l', mktime(0, 0, 0, $mon, 1, $year)); // finds the day of the first of the month.
 return array_search($dow, $start); // returns the day number of the first of the month.


  Hopefully with the comments that is not too hard to understand. I think the most difficult part of that file to understand for a beginner programmer is probably the array. In this array, the numbers are the Key and the week day is the Value. So, the array_search() function is searching for the day of the week within that array and will return the day number. Example: If you run this for December 2010, it would return 4, because the first of the month fell on a Wednesday. In PHP, Sunday is the first day of the week, of course you could modify the script so that the first day of the week is whatever you want it to be, but we will be using Sunday as day 1 to make it easier for beginners/novices.

  Now, once that functions file is created, it's time to create the actual calendar. We will be using a For loop with If-Else statements to do this. The calendar will also be contained within a table, so those of you that are well versed in CSS may make some nifty looking calendar's. I have named the calendar file calendar.php.



include("functions.php"); // include the functions file we created

$yr = date('Y');
$mon = date('n');
$tmon = date('F'); // returns the full text month name. Example: December

print "<table><th colspan='7'>".$tmon."</th>"; // prints the name of the month in a table header.
print "<tr>";
print "<td>Sun</td> <td>Mon</td> <td>Tue</td> <td>Wed</td> <td>Thu</td> <td>Fri</td> <td>Sat</td>";
print "</tr>";

for($p=1;$p<startDay();$p++) {
  print "<td></td>"; // for every day before the first of the month, add a blank table column.

for($i=1;$i<=getDays();$i++) {
  if($i==9-startDay() || $i==16-startDay() || $i==23-startDay() || $i==30-startDay()) { 
    print "<tr>"; // create a new row every Sunday.
  if($i==date('j')) {
    print "<td><b>".date('d', mktime(0, 0, 0, $mon, $i, $yr))."</td>"; // make today's date bold.
  } else {
    print "<td>".date('d', mktime(0, 0, 0, $mon, $i, $yr))."</td>";
  if($i==8-startDay() || $i==15-startDay() || $i==22-startDay() || $i==29-startDay()) {
    print "<tr>"; // end the row after every Saturday.

print "</tr></table>";


 Now, this file is a little harder to understand and I think that I commented the hard parts pretty well. I will now go through and explain the for loops, if-else statements, and the date(mktime()) functions.

  The first for loop is rather simple. The purpose of this for loop is to add empty table cells before the first day of the month. So, if the first day of the month is Thursday, then Sunday-Wednesday will all have empty cells.

  The next for loop runs until every day of the month has been filled in and this for loop contains some if-else statements that may be a little confusing.

  The first if statement creates a new table row before every Sunday. When you look at this, you may notice that I have 9-startDay() as the first new week. This is because the first day of the month is 1 and add 7 to that for the number of days in a week, then add 1 because you want the first day of the following week to be the start of the new row, and you get 9. Then you just add 7 to that number for each new week. The -startDay() is because you want to subtract those numbers by the starting day of the month to get the actual date of each Sunday. Example: In December, the first Sunday was the 5th. You get this because 9-4 (Wednesday is the 4th day of the week) = 5.

  The next if-else statement places every day of the month in 0x form (01, 02, 03, etc.) into it's own table cell and makes the current day bold. You will notice that we are using the mktime() function inside of the date. This is basically a check that every date is going into the correct weekday column. You can use this to check the correct days by changing the "d" to a "D" which will then display day names as three letters. If any of the days are wrong, you know you have a problem somewhere else in the code. The mktime() function can be used for many things, such as a countdown script or happy birthday script.

  The last if statement is the opposite of the first one. This if statement ends the row after every Saturday is displayed. Apply the same logic from the first if statement to this one and you will understand it perfectly.

  Finally, you close the if-else statements, the for loops, and you print the close table row and close table tags to complete your very own calendar. This is neither the best way to do this, nor is it the worst way, but I thought it would be the easiest way for a beginner to learn some valuable PHP functions and hopefully have a little fun. For full information on the functions used in this program, visit and search for the function you're curious about in the function list. The functions with the most uses are date and mktime, so be sure to give those a search. Program well!

Dig this tutorial?
Thank the author by sending him a few P2L credits!


This author is too busy writing tutorials instead of writing a personal profile!
View Full Profile Add as Friend Send PM
Pixel2Life Home Advanced Search Search Tutorial Index Publish Tutorials Community Forums Web Hosting P2L On Facebook P2L On Twitter P2L Feeds Tutorial Index Publish Tutorials Community Forums Web Hosting P2L On Facebook P2L On Twitter P2L Feeds Pixel2life Homepage Submit a Tutorial Publish a Tutorial Join our Forums P2L Marketplace Advertise on P2L P2L Website Hosting Help and FAQ Topsites Link Exchange P2L RSS Feeds P2L Sitemap Contact Us Privacy Statement Legal P2L Facebook Fanpage Follow us on Twitter P2L Studios Portal P2L Website Hosting Back to Top