The following snippet of code displays the current calendar month in an HTML table.
First we will get some information about today’s date.
$day_num=date("j"); //If today is September 29, $day_num=29
$month_num = date("m"); //If today is September 29, $month_num=9
$year = date("Y"); //4-digit year
$date_today = getdate(mktime(0,0,0,$month_num,1,$year)); //Returns array of date info for 1st day of this month
$month_name = $date_today["month"]; //Example: "September" - to label the Calendar
$first_week_day = $date_today["wday"]; //"wday" is 0-6, 0 being Sunday. This is for day 1 of this month
Refer to PHP: getdate – Manual for more information on this function.
Next we are going to figure out which day is the FINAL day of the month.
$cont = true;
$today = 27; //The last day of the month must be >27, so start here
while (($today <= 32) && ($cont)) //At 32, we have to be rolling over to the next month
//Iterate through, incrementing $today
//Get the date information for the (hypothetical) date $month_num/$today/$year
$date_today = getdate(mktime(0,0,0,$month_num,$today,$year));
//Once $date_today's month ($date_today["mon"]) rolls over to the next month, we've found the $lastday
if ($date_today["mon"] != $month_num)
$lastday = $today - 1; //If we just rolled over to the next month, need to subtract 1 to get our $lastday
$cont = false; //This kicks us out of the while loop
Now we have the final day of the month in
$lastday. Let’s start some output, starting with a table with column headers for each day of the week.
<table cellspacing=0 cellpadding=5 frame='all' rules='all' style='border:#808080 1px solid;'>
The first week of the month is a special case because not all cells will necessarily be filled, since they may be days for last month.
$day = 1; //This variable will track the day of the month
$wday = $first_week_day; //This variable will track the day of the week (0-6, with Sunday being 0)
$firstweek = true; //Initialize $firstweek variable so we can deal with it first
while ( $day <= $lastday) //Iterate through all days of the month
if ($firstweek) //Special case - first week (remember we initialized $first_week_day above)
echo "<tr align=left>";
for ($i=1; $i<=$first_week_day; $i++)
echo "<td> </td>"; //Put a blank cell for each day until you hit $first_week_day
$firstweek = false; //Great, we're done with the blank cells
if ($wday==0) //Start a new row every Sunday
echo "<tr align=left>";
Now we are just outputting a cell for each day of the week showing $day.
if($day==$day_num) echo " bgcolor='yellow'"; //highlight TODAY in yellow
echo "</tr>"; //If today is Saturday, close this row
$wday++; //Increment $wday
$wday = $wday % 7; //Make sure $wday stays between 0 and 6 (so when $wday++ == 7, this will take it back to 0)
$day++; //Increment $day
Now all that’s left to do is end the final row of the month with blank cells.
while($wday <=6 ) //Until we get through Saturday
echo "<td> </td>"; //Output an empty cell
Obviously, there are lots of things you can do with a calendar – put a Month dropdown at the top to switch to a different month, make each day a link to see a calendar of events for a single day, and so on.