PDA

View Full Version : Fun With Calendars



JayT
09-16-2007, 09:32 AM
Here are two simple calendar functions you might find useful in some programs.

One creates a calendar within a table and the other function creates a plain text calendar within a PRE block.

A simple PHP Print() command will display the calendars.

The PHP code tester program I posted elswhere on this forum the other day is ideal for testing functions like these.

Both of these functions should work in PHP v4 and v5.




<?PHP





// Call the function for the test year and month.
// If a date is given, then it is highlighted.

print Greg_Cal_Table(2007, *0, **);




/*

This function returns a calendar for any Gregorian
calendar date in the form of an HTML table. This
function is also valid for proleptic Gregorian dates.

$YNum = Year number argument (* to *2767)
$MNum = Month number argument (* to *2)

$JD* = JD number of *st of month
$HDay = Date to highlight within calendar (optional)
$MName = Name of given month
$DoW* = Day of week code (0 to 6) for *st of month
$NumDays = Number of days in the given month

The optional parameter $HDay is the day of the month
to be highlighted in the calendar display. If not
given, then no date will be highlighted.

*/


Function Greg_Cal_Table($YNum, $MNum, $HDay=0)

{

// Error if invalid date.
if (!CheckDate($MNum, *, $YNum))
{
return "<TABLE BGCOLOR='black' CELLPADDING='2'>\n
<TR><TD BGCOLOR='yellow' ALIGN='center'>ERROR: Invalid Date Element(s)</TD></TR>\n
</TABLE>";
}

// Get parameters needed to create calendar table.
$JD* = GregorianToJD ($MNum, *, $YNum);
$MName = JDMonthName ($JD*, *);
$DoW* = JDDayOfWeek ($JD*, 0);
$NumDays = Cal_Days_In_Month (CAL_GREGORIAN, $MNum, $YNum);

// Begin calendar table construction.
$CalTable =
"<TABLE BGCOLOR='black' CELLPADDING='2'>\n
<TR BGCOLOR='cyan'>
<TD COLSPAN='7' ALIGN='center'><B>$YNum $MName</B></TD>\n
</TR>
<TR BGCOLOR='LightCyan'>
<TD WIDTH='*6' ALIGN='center'>Sun</TD><TD WIDTH='*6' ALIGN='center'>Mon</TD><TD WIDTH='*6' ALIGN='center'>Tue</TD><TD WIDTH='*6' ALIGN='center'>Wed</TD><TD WIDTH='*6' ALIGN='center'>Thu</TD><TD WIDTH='*6' ALIGN='center'>Fri</TD><TD WIDTH='*6' ALIGN='center'>Sat</TD>
</TR>
<TR>";

// Fill any blank cells at beginning of calendar
// matrix if month does NOT start on a Sunday.
for ($i=*; $i <= $DoW*; $i++)
{$CalTable .= "<TD> </TD>\n";}

// Fill in calendar day number cells.
for ($day=*; $day <= $NumDays; $day++)

{
// if ($day != $HDay) {$BgC = "white";} else {$BgC = "yellow";}

$BgC = ($day != $HDay)? "white" : "yellow";

if ($BgC == "yellow")
{$Bold* = "<B>"; $Bold2 = "</B>";}
else
{$Bold* = $Bold2 = "";}

$CalTable .= "<TD BGCOLOR='$BgC' ALIGN='center'>$Bold*$day$Bold2";

if (($day + $DoW* ) &#*7; 7 == 0)
{$CalTable .= "</TD>\n</TR>\n<TR>\n";}
else
{$CalTable .= "</TD>\n";}
}

// Close out the table.
$CalTable .= "</TR>\n";
$CalTable .= "</TABLE>\n";

// Patch to delete redundant empty table row.
$CalTable = Str_Replace("<TR>\n</TR>", "", $CalTable);

// Done.
return $CalTable;

} // End of Greg_Cal_Table()


?>






Here is the plain text version.



<?PHP

// Call the function for test year and month.

print Greg_Cal_Text (2007, *);






function Greg_Cal_Text($YNum, $MNum)

{

// Error if invalid date.
if (!CheckDate($MNum, *, $YNum))
{
return "ERROR: Invalid date element(s)";
}

// Get parameters needed to create calendar table.
$JD* = GregorianToJD ($MNum, *, $YNum);
$MName = JDMonthName ($JD*, *);
$DoW* = JDDayOfWeek ($JD*, 0);
$NumDays = Cal_Days_In_Month (CAL_GREGORIAN, $MNum, $YNum);

$PadSpaces = Str_Repeat(" ", 20 - (StrLen($MName)+StrLen($YNum)));
$line = ""; // Str_Repeat("=", 20);

$CalStr = "";
$WDays = "Su Mo Tu We Th Fr Sa";

for ($i=*; $i <= $DoW*; $i++) {$CalStr .= "&nbsp;&nbsp;&nbsp;";}

for ($d=*; $d <= $NumDays; $d++)
{
if ($d < *0) {$p = "&nbsp;";} else {$p = "";}
$CalStr .= $p . "$d ";
if (($d + $DoW* ) % 7 == 0) {$CalStr .= "\n";}
}

$calendar = trim("$line\n$YNum$PadSpaces$MName\n\n$WDays\n$CalStr\n$line");

// -----
// Done.

return "<PRE>$calendar</PRE>";

} // End of Greg_Cal_Text()

?>