<?php

function showMonth($month, $year)
{
$date = mktime(12, 0, 0, $month, 1, $year);
$daysInMonth = date("t", $date);
// calculate the position of the first day in the calendar (sunday = 1st column, etc)
$offset = date("w", $date);
$rows = 1;
 
echo "<h1>Displaying calendar for " . date("F Y", $date) . "</h1>";
 
echo "<table border=1>";
echo "<tr><th>Su</th><th>M</th><th>Tu</th><th>W</th><th>Th</th><th>F</th><th>Sa</th></tr>";
echo "<tr>";
 
for($i = 1; $i <= $offset; $i++)
{
echo "<td></td>";
}
for($day = 1; $day <= $daysInMonth; $day++)
{
if( ($day + $offset - 1) % 7 == 0 && $day != 1)
{
echo "</tr><tr>";
$rows++;
}
 
echo "<td>" . $day . "</td>";
}
while( ($day + $offset) <= $rows * 7)
{
echo "<td></td>";
$day++;
}
echo "</tr>";
echo "</table>";
}

?>


<html>
<head><title>Calendar</title></head>
<body>
<?php
 showMonth(7, 2005); // July 2005
showMonth(1, 1980); // January 1980
showMonth(10, 2014); // December 2012 ?>
</body>
</html>