How to Build a Simple PHP Calendar

of 05

Getting Calendar Variables

woman working on computer

 gilaxia/Getty Images

PHP calendars can be useful. You can do things as simple as showing the date, and as complex as setting up an online booking system. This article shows how to generate a simple PHP calendar. When you understand how to do this, you will be able to apply the same concepts to complex calendars you may need.


The first part of the code sets some variables that are needed later in the script. The first step is to find out what the current date is using the time () function. Then, you can use the date () function to format the date appropriately for the $day, $month and $year variables. Finally, the code generates the name of the month, which is the title of the calendar.

of 05

Days of the Week

//Here you find out what day of the week the first day of the month falls on
$day_of_week = date('D', $first_day) ;
//Once you know what day of the week it falls on, we know how many blank days occur before it. If the first day of the week is a
Sunday, then it is zero
case "Sun": $blank = 0; break;
case "Mon": $blank = 1; break;
case "Tue": $blank = 2; break;
case "Wed": $blank = 3; break;
case "Thu": $blank = 4; break;
case "Fri": $blank = 5; break;
case "Sat": $blank = 6; break;
//We then determine how many days are in the current month
$days_in_month = cal_days_in_month(0, $month, $year) ;

Here you take a close look at the days of the month and prepare to make the calendar table. The first thing is to determine on which day of the week the first of the month falls. With that knowledge, you use the switch () function to determine how many blank days are needed in a calendar before the first day.

Next, count the total days of the month. When you know how many blank days are needed and how many total days are in the month, the calendar can be generated.

of 05

Headings and Blank Calendar Days

//Here you start building the table heads
echo "";
echo " $title $year ";
echo "SMTWTFS";
//This counts the days in the week, up to 7
$day_count = 1;
echo "";
//first you take care of those blank days
while ( $blank > 0 )
echo "";
$blank = $blank-1;

The first part of this code echoes the table tags, the month name and the headings for the days of the week. Then it starts a while loop that echoes empty table details, one for each blank day to count down. When the blank days are done, it stops. At the same time, the $day_count is going up by 1 each time through the loop. This keeps count to prevent putting more than seven days in a week.

of 05

Days of the Month

//sets the first day of the month to 1
$day_num = 1;
//count up the days, until you've done all of them in the month
while ( $day_num $day_num ";
//Make sure you start a new row every week
if ($day_count > 7)
echo "";
$day_count = 1;

Another while loop fills in the days of the month, but this time it counts up to the last day of the month. Each cycle echoes a table detail with the day of the month, and it repeats until it reaches the last day of the month.

The loop also contains a conditional statement. This checks if the days of the week have reached 7—the end of the week. If it has, it starts a new row and resets the counter back to 1.

of 05

Finishing the Calendar

//Finally you finish out the table with some blank details
if needed
while ( $day_count >1 && $day_count ";
echo ""; 

One last while loop finishes the calendar. This one fills in the rest of the calendar with blank table details if needed. Then the table is closed and the script is complete.