xymon
+ Reply to Thread
Results 1 to 1 of 1

Thread: Fun With Calendars

  1. #1
    Join Date
    Aug 2007
    Posts
    122

    Fun With Calendars

    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.


    Code:
    <?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.

    Code:
    <?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()
    
    ?>
    Last edited by JayT; 09-16-2007 at 09:39 AM.
    Oh to be free, so blissfully free, of the ravages of intelligence, there is no greater joy! - The Cweationist's Cweed

    All that is necessary for evil to triumph is a good PR firm.
    Very funny, Scotty. Now beam down my clothes!

+ Reply to Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts