Wednesday, September 3, 2008

Project Calendar - Month-View Generator

Here is the code of the function that generates the calendar, passing two variables (static or user generated).

function renderCalendar($strMonth, $strYear) {
$date = mktime(12, 0, 0, $strMonth, 1, $strYear);
$daysInMonth = date("t", $date);
$dayOffset = date("w", $date);

$calendarWeeks = ceil(($daysInMonth + $dayOffset) / 7) - 1; //how many weeks (rows) in the month

echo "*****HTML Code for Table *****";

for ($rowIndex = 0; $rowIndex <= $calendarWeeks; $rowIndex++) {
echo "*****HTML Code for Table Row *****";
for ($colIndex = 1; $colIndex <= 7; $colIndex++) {
$currentDay = ($colIndex - $dayOffset) + ($rowIndex * 7);

if ($currentDay < 1 or $currentDay > $daysInMonth) {
echo "*****HTML Code for empty cell *****";
} else {
echo "*****HTML Code for date cell *****";
}
}
echo "*****HTML Code for closing Table Row *****";
}
}


You can check the result here.