Original Post
About a year ago, this was very difficult to achieve. I just decided to drop the idea. Last night, I picked up the idea, started coding this morning, for about 30 minutes and it worked! And I am very happy! And it's very easy to use! Well, all you have to do is call the function displayCalendar(month, year) where month is a string representing the month you want, e.g. "January", and year is an integer. I did not do any error checking for invalid years (hopefully, anyone using it will be sensible enough not to go beyond the limits? Although I know there are people who are always willing to break code [grin]) It uses tables for layout... uh... it's not feasible for you to use absolute positioning with this, except you're insane. Anyways, here's the code:
<?
// PHP Calendar Helper Functions/Script
// Copyright (C) 2005 Akinwale Ariwodola
// Default styles - add this to your stylesheet or style tag
// .calendarBorder { background: #999 }
// .calendar TD { background: #FFF; font-family: Verdana, Arial, sans-serif;
// font-size: 10px; text-align: right }
// .calendarDays TD { background: #DDD; font-family: Verdana, Arial, sans-serif;
// font-size: 10px; font-weight: bold; text-align: right }
$monthsOfYear = array("January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December");
$daysOfWeek = array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");
$daysOfWeekS = array("S", "M", "T", "W", "T", "F", "S");
function getFirstDayOfMonthIndex($monthStr, $year)
{
global $monthsOfYear, $daysOfWeek;
$monthIndex = 0;
for($i = 0; $i < count($monthsOfYear); $i++)
{
if(strcmp($monthsOfYear[$i], $monthStr) == 0)
{
$monthIndex = $i;
}
}
$firstdayStr = date("l", mktime(0, 0, 0, ($monthIndex + 1), 1, $year));
for($index = 0; $index < count($daysOfWeek); $index++)
{
if(strcmp($daysOfWeek[$index], $firstdayStr) == 0)
{
return $index;
}
}
}
function getCurrentYear()
{
return intval(date("Y"));
}
function getCurrentMonthStr()
{
return date("F");
}
function getCurrentDayIndex()
{
global $daysOfWeek;
for($i = 0; $i < count($daysOfWeek); $i++)
{
if(strcmp($daysOfWeek[$i], date("l")) == 0)
{
return $i;
}
}
}
function getCurrentMonthIndex()
{
global $monthsOfYear;
for($i = 0; $i < count($monthsOfYear); $i++)
{
if(strcmp($monthsOfYear[$i], date("F")) == 0)
{
return $i;
}
}
}
function isLeapYear($year)
{
if($year % 4 == 0 && (($year % 100 != 0) || ($year % 400 == 0)))
{
return 1;
}
return 0;
}
function getNumDaysInMonth($monthStr, $year)
{
$retval = 0;
switch($monthStr)
{
case "January": $retval = 31; break;
case "February": $retval = (isLeapYear($year)) ? 29 : 28; break;
case "March": $retval = 31; break;
case "April": $retval = 30; break;
case "May": $retval = 31; break;
case "June": $retval = 30; break;
case "July": $retval = 31; break;
case "August": $retval = 31; break;
case "Septebmer": $retval = 30; break;
case "October": $retval = 31; break;
case "November": $retval = 30; break;
case "December": $retval = 31; break;
}
return $retval;
}
// simply call this function with the month string, and the year you want to display
function displayCalendar($monthStr, $year)
{
$numdays = getNumDaysInMonth($monthStr, $year);
$firstdayIndex = getFirstDayOfMonthIndex($monthStr, $year);
$preCols = 0;
echo "<table class=\"calendarBorder\" cellspacing=\"0\" cellpadding=\"0\"><tr><td width=\"100%\">\n";
echo "<table class=\"calendar\" cellspacing=\"1\" cellpadding=\"3\">\n";
echo "<tr class=\"calendarDays\"><td>S</td> <td>M</td> <td>T</td> <td>W</td>
<td>T</td> <td>F</td> <td>S</td></tr>\n";
for($pre = 0; $pre < $firstdayIndex; $pre++)
{
if($pre == 0)
{
echo "<tr>\n";
}
echo "<td></td>\n";
$preCols += 1;
}
for($i = 0; $i < $numdays; $i++)
{
if(($i + $preCols) % 7 == 0)
{
echo "</tr>\n<tr>";
}
echo "<td style=\"text-align: right\">" . ($i + 1). "</td>\n";
}
echo "</table></td></tr></table>\n";
}
?>