O'Reilly logo

Learning PHP 5 by David Sklar

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Displaying a Calendar

This section puts the date and time functions to work in displaying a calendar. The show_form( ) function in Example 9-17 displays a form that asks for a month and year. The process_form( ) function hands those values off to the show_calendar( ) function, which does the real work of printing a calendar grid for a particular month.

The structure of the if( ) statement that controls show_form( ) , validate_form( ), and process_form( ) is different in Example 9-17 than in previous form examples. That's because we want to display the form above the calendar. Usually, if the form data is valid, show_form( ) is not called—only process_form( ) is. But here, show_form( ) is called before process_form( ) so that the form is displayed above the calendar and the user can pick another month and year to view.

Similarly, the call to show_form( ) that happens when the form has not been submitted (when there is no $_POST['_submit_check'] parameter) is followed by a call to show_calendar( ) to display the calendar for the current month the first time the page is loaded.

Example 9-17. Printing a calendar

<?php // Use the form helper functions defined in Chapter 6 require 'formhelpers.php'; $months = array(1 => 'January', 2 => 'February', 3 => 'March', 4 => 'April', 5 => 'May', 6 => 'June', 7 => 'July', 8 => 'August', 9 => 'September', 10 => 'October', 11 => 'November', 12 => 'December'); $years = array( ); for ($year = date('Y') - 1, $max_year = date('Y') + 5; $year < $max_year; ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required