SakhaliaNetHome PageSahara TerritoryVorKutaAcceptance of cookiesAcceptance of cookies

PHP Tutorial :: Date and time (IV)

PHP Example #98

Showing a calendar

This example builds a calendar for the month and year selected by the user.

<?php
require 'scripts/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; $year ++) {
$years[$year] = $year;
}
if ($_POST['_submit_check']) {
if ($errors = validate_form()) {
show_form($errors);
} else {
show_form();
process_form();
}
} else {
// When nothing is sent, show the form and then a calendar for the
// current month
show_form();
show_calendar(date('n'), date('Y'));
}
function validate_form() {
global $months, $years;
$errors = array();
if (! array_key_exists($_POST['month'], $months)) {
$errors[] = 'Select a valid month.';
}
if (! array_key_exists($_POST['year'], $years)) {
$errors[] = 'Select a valid year.';
}
return $errors;
}
function show_form($errors = '') {
global $months, $years, $this_year;
// If the form is sent, obtain the default values for the variables sent
if ($_POST['_submit_check']) {
$defaults = $_POST;
} else {
// If not, set our own default values: the current month and year
$defaults = array('year' => date('Y'), 'month' => date('n'));
}
if ($errors) {
print 'You need to correct the following errors: <ul><li>';
print implode('</li><li>', $errors);
print '</li></ul>';
}
print '<form method="post" action="' . $_SERVER['PHP_SELF'] . '">';
input_select('month', $defaults, $months);
input_select('year', $defaults, $years);
input_submit('submit', 'Show Calendar');
print '<input type="hidden" name="_submit_check" value="1"/>';
print '</form><br/>';
}
function process_form() {
show_calendar($_POST['month'], $_POST['year']);
}
function show_calendar($month, $year) {
global $months;
$weekdays = array('Su', 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa');
// Finds the timestamp value for the midnight of the first day of the month
$first_day = mktime(0, 0, 0, $month, 1, $year);
// How many days has a month?
$days_in_month = date('t', $first_day);
// Which day of the week (numeric) is the first day of the month?
// Needs it to place the first cell of the table in the appropriate place
$day_offset = date('w', $first_day);
// Prints the header of the table and the row with the days of the week
print '<table border="0" cellspacing="0" cellpadding="2">';
print '<tr><th colspan="7" align="center">';
print $months[$month] . ' ' . $year . '</th></tr>';
print '<tr><td align="center">';
print implode('</td><td align="center">', $weekdays);
print '</td></tr>';
// If the first day of the month is for instance a Tuesday, then it needs
// to place empty table cells below "Su" and "Mo" in the first row so
// the cell corresponding to the first day would be placed below "Tu"
if ($day_offset > 0) {
for ($i = 0; $i < $day_offset; $i ++) { print '<td> </td>'; }
}
// Prints a table cell for each day of the month
for ($day = 1; $day <= $days_in_month; $day ++) {
print '<td align="center">' . $day . '</td>';
$day_offset ++;
// If the cell was the seventh one in the row, then end the row and
// restore $day_offset
if ($day_offset == 7) {
$day_offset = 0;
print "</tr>\n";
// If there are still more days, then begin a new row
if ($day < $days_in_month) {
print '<tr>';
}
}
}
// In these moments, a table cell for each day of the month has been printed.
// If the last day of the month is not a Saturday, then the last row of the
// table has to be joint with some empty cells at the end of the row
if ($day_offset > 0) {
for ($i = $day_offset; $i < 7; $i ++) {
print '<td> </td>';
}
print '</tr>';
}
print '</table>';
}
?>

June 2017
SuMoTuWeThFrSa
    123
45678910
11121314151617
18192021222324
252627282930