PHP CookBook Free Open Book

PHP CookBook

Previous Section Next Section

Recipe 3.17 Program: Calendar

The pc_calendar() function shown in Example 3-4 prints out a month's calendar, similar to the Unix cal program. Here's how you can use the function:

// print the calendar for the current month
list($month,$year) = explode(',',date('m,Y'));
pc_calendar($month,$year);

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.

Example 3-4. pc_calendar( )
<?php
function pc_calendar($month,$year,$opts = '') {
    // set default options //
    if (! is_array($opts)) { $opts = array(); }
    if (! isset($opts['today_color'])) { $opts['today_color'] = '#FFFF00'; }
    if (! isset($opts['month_link'])) {
        $opts['month_link'] = 
            '<a href="'.$_SERVER['PHP_SELF'].'?month=%d&year=%d">%s</a>';
    }
    
    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,'&lt;');
    
    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,'&gt;');
    
?>
<table border="0" cellspacing="0" cellpadding="2" align="center">
        <tr>
                <td align="left">
                        <?php print $prev_month_link ?>
                </td>
                <td colspan="5" align="center">
                <?php print strftime('%B %Y',mktime(0,0,0,$month,1,$year)); ?>
                </td>
                <td align="right">
                        <?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 align="center">'.$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 sprintf('<td align="center" bgcolor="%s">%d</td>',
                              $opts['today_color'],$day);
            } else {
                print sprintf('<td align="center">%d</td>',$day);
            }
        } else {
            print sprintf('<td align="center">%d</td>',$day);
        }
        $day_offset++;

        // start a new row each week // 
        if ($day_offset == 7) {
            $day_offset = 0;
            print "</tr>\n";
            if ($day < $totaldays) { print '<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. The color that the current day is highlighted with can be passed as an RGB value in $opts['today_color']. This defaults to #FFFF00, bright yellow. Also, 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.

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.

    Previous Section Next Section
    Index: [SYMBOL][A][B][C][D][E][F][G][H][I][J][K][L][M][N][O][P][Q][R][S][T][U][V][W][X][Z]


         Main Menu
    Main Page
    Table of content
    Copyright
    Preface
    Chapter 1. Strings
    Chapter 2. Numbers
    Chapter 3. Dates and Times
    3.1 Introduction
    Recipe 3.2 Finding the Current Date and Time
    Recipe 3.3 Converting Time and Date Parts to an Epoch Timestamp
    Recipe 3.4 Converting an Epoch Timestamp to Time and Date Parts
    Recipe 3.5 Printing a Date or Time in a Specified Format
    Recipe 3.6 Finding the Difference of Two Dates
    Recipe 3.7 Finding the Difference of Two Dates with Julian Days
    Recipe 3.8 Finding the Day in a Week, Month, Year, or the Week Number in a Year
    Recipe 3.9 Validating a Date
    Recipe 3.10 Parsing Dates and Times from Strings
    Recipe 3.11 Adding to or Subtracting from a Date
    Recipe 3.12 Calculating Time with Time Zones
    Recipe 3.13 Accounting for Daylight Saving Time
    Recipe 3.14 Generating a High-Precision Time
    Recipe 3.15 Generating Time Ranges
    Recipe 3.16 Using Non-Gregorian Calendars
    Recipe 3.17 Program: Calendar
    Chapter 4. Arrays
    Chapter 5. Variables
    Chapter 6. Functions
    Chapter 7. Classes and Objects
    Chapter 8. Web Basics
    Chapter 9. Forms
    Chapter 10. Database Access
    Chapter 11. Web Automation
    Chapter 12. XML
    Chapter 13. Regular Expressions
    Chapter 14. Encryption and Security
    Chapter 15. Graphics
    Chapter 16. Internationalization and Localization
    Chapter 17. Internet Services
    Chapter 18. Files
    Chapter 19. Directories
    Chapter 20. Client-Side PHP
    Chapter 21. PEAR
    Colophon
    Index


    More Books
    PHP Hacks
    Processing Xml With Java - A Guide To Sax, Dom, Jdom, Jaxp, And Trax
    The Koran (Holy Qur'an)
    Macromedia Flash 8 Bible
    Search Engine Optimization for Dummies
    YouTube Traffic
    PHP 5 for Dummies
    Harry Potter and The Chamber of Secrets
    Harry Potter and the Sorcerer's Stone
    The Pilgrim's Progress
    Wireless Hacks
    Flash Hacks. 100 Industrial-Strength Tips & Tools
    PayPal Hacks. 100 Industrial-Strength Tips and Tools
    Amazon Hacks
    Pdf Hacks
    The Da Vinci Code
    Google Hacks
    The Holy Bible
    Windows XP For Dummies
    Harry Potter and the Half-Blood Prince
    Seo Book
    Upgrading and Repairing Networks
    Macromedia Dreamweaver 8 UNLEASHED
    Windows XP Annoyances
    Windows XP Hacks
    Microsoft Windows XP Power Toolkit
    Teach Yourself MS Office In 24Hours
    iPod & iTunes Missing Manual
    PC Hacks 100 Industrial-Strength Tips and Tools
    PC Overclocking, Optimization, and Tuning - 2th Edition
    PC Hardware In A Nutshell 3rd Edition
    PC Hardware in a Nutshell, 2nd Edition
    Upgrading and Repairing PCs
    Google for Dummies
    MySQL Cookbook
    Teach Yourself Macromedia Flash 8 In 24 Hours
    PHP CookBook
    Sams Teach Yourself JavaScript in 24 Hours
    PHP5 Manual
    Free Games Paper Airplanes
    500 Juegos Gratis 500 Giochi Gratis 500 Jeux Gratuits 500 Jogos Gratis 500 Kostenlose Spiele