1. Computing

Simple PHP Calendar

By , About.com Guide

2 of 5

Days of the Week
 //Here we find out what day of the week the first day of the month falls on 
 $day_of_week = date('D', $first_day) ; 

//Once we know what day of the week it falls on, we know how many blank days occure before it. If the first day of the week is a Sunday then it would be zero
switch($day_of_week){
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 we take a closer look at the days of the month and prepare to make our calendar table. The first thing we do is determine what day of the week the first of the month falls. Once we know that, we use the switch () function to determine how many blank days we need in our calendar before the first day.

Next we count the total days of the month. Now that we know how many 'blank' days we need, and how many total days are in the month we can start to generate our calendar.

[See working calendar example]

Related Video
How to Create a PHP Calendar
  1. About.com
  2. Computing
  3. PHP / MySQL
  4. Step By Steps
  5. PHP Calendar Tutorial - PHP Calendar Guide - PHP Calendar Code

©2013 About.com. All rights reserved.