Recipe 3.17. Program: Calendar


The pc_calendar( ) function shown in Example 3-47 prints out a month's calendar, similar to the Unix cal program. Example 3-46 shows how you can use the function, including default styles for its layout.

Using pc_calendar( )

<style type="text/css"> .prev { text-align: left; } .next { text-align: right; } .day, .month, .weekday { text-align: center; } .today { background: yellow; } .blank { } </style> <?php // print the calendar for the current month if a month // or year isn't in the query string $month = isset($_GET['month']) ? intval($_GET['month']) : date('m'); $year = isset($_GET['year']) ? intval($_GET['year']) : date('y'); ?>

The pc_calendar( ) function prints out a table with a month's calendar in it. It provides links to the previous and next month and highlights the current day, as shown in Example 3-47.

pc_calendar( )

<?php function pc_calendar($month,$year,$opts = '') {     // set default options     if (! is_array($opts)) { $opts = array(); }     if (! isset($opts['id'])) { $opts['id'] = 'calendar'; }     if (! isset($opts['month_link'])) {         $opts['month_link'] =             '<a href="'.$_SERVER['PHP_SELF'].'?month=%d&amp;year=%d">%s</a>';     }     $classes = array();     foreach (array('prev','month','next','weekday','blank','day','today') as $class) {         if (isset($opts[$class.'_class'])) {             $classes[$class] = htmlentities($opts[$class.'_class']);         } else {             $classes[$class] = $class;         }     }     list($this_month,$this_year,$this_day) = split(',',strftime('%m,%Y,%d'));     $day_highlight = (($this_month == $month) && ($this_year == $year));     list($prev_month,$prev_year) =         split(',',strftime('%m,%Y',mktime(0,0,0,$month-1,1,$year)));     $prev_month_link = sprintf($opts['month_link'],$prev_month,$prev_year,'&laquo;');     list($next_month,$next_year) =         split(',',strftime('%m,%Y',mktime(0,0,0,$month+1,1,$year)));     $next_month_link = sprintf($opts['month_link'],$next_month,$next_year,'&raquo;'); ?> <table >         <tr>                 <td >                         <?php print $prev_month_link ?>                 </td>                 <td  colspan="5">                 <?php print strftime('%B %Y',mktime(0,0,0,$month,1,$year)); ?>                 </td>                 <td >                         <?php print $next_month_link ?>                 </td>         </tr> <?php     $totaldays = date('t',mktime(0,0,0,$month,1,$year));     // print out days of the week     print '<tr>';     $weekdays = array('Su','Mo','Tu','We','Th','Fr','Sa');     while (list($k,$v) = each($weekdays)) {         print '<td >'.$v.'</td>';     }     print '</tr><tr>';     // align the first day of the month with the right week day     $day_offset = date("w",mktime(0, 0, 0, $month, 1, $year));     if ($day_offset > 0) {         for ($i = 0; $i < $day_offset; $i++) {             print '<td >&nbsp;</td>';         }     }     $yesterday = time() - 86400;     // print out the days     for ($day = 1; $day <= $totaldays; $day++) {         $day_secs = mktime(0,0,0,$month,$day,$year);         if ($day_secs >= $yesterday) {             if ($day_highlight && ($day == $this_day)) {                 print '<td >' . $day . '</td>';             } else {                 print '<td >' . $day . '</td>';             }         } else {             print '<td >' . $day .'</td>';         }         $day_offset++;         // start a new row each week //         if ($day_offset == 7) {             $day_offset = 0;             if ($day < $totaldays) { print "</tr>\n<tr>"; }         }     }     // fill in the last week with blanks //     if ($day_offset > 0) { $day_offset = 7 - $day_offset; }     if ($day_offset > 0) {         for ($i = 0; $i < $day_offset; $i++) {             print '<td >&nbsp;</td>';         }     }     print '</tr></table>'; } ?>

The pc_calendar( ) function begins by checking options passed to it in $opts. You can pass a printf( )-style format string in $opts['month_link'] to change how the links to the previous and next months are printed as well as an id attribute for the table. The id defaults to calendar if not specified.

Additionally, you can pass in class names to use for various elements in the layout. The option names for these classes are prev_class, month_class, next_class, weekday_class, blank_class, day_class, and today_class. The default values are prev, month, next, weekday, blank, day, and today. Example 3-46 includes styles that provide a basic pleasant layout for the table, including highlighting the current day in yellow.

Next, the function sets $day_highlight to true if the month and year for the calendar match the current month and year. The links to the previous month and next month are put into $prev_month_link and $next_month_link using the format string in $opts['month_link'].

pc_calendar( ) then prints out the top of the HTML table that contains the calendar and a table row of weekday abbreviations. Using the day of the week returned from strftime('%w'), blank table cells are printed so the first day of the month is aligned with the appropriate day of the week. For example, if the first day of the month is a Tuesday, two blank cells have to be printed to occupy the slots under Sunday and Monday in the first row of the table.

After this preliminary information has been printed, pc_calendar( ) loops through all the days in the month. It prints a plain table cell for most days, but a table cell with a different background color for the current day. When $day_offset reaches 7, a week has completed, and a new table row needs to start.

Once a table cell has been printed for each day in the month, blank cells are added to fill out the last row of the table. For example, if the last day of the month is a Thursday, two cells are added to occupy the slots under Friday and Saturday. Last, the table is closed, and the calendar is complete .




PHP Cookbook, 2nd Edition
PHP Cookbook: Solutions and Examples for PHP Programmers
ISBN: 0596101015
EAN: 2147483647
Year: 2006
Pages: 445

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net