SakhaliaNetHome PageHistory of the RailwayVorKutaAcceptance of cookiesAcceptance of cookies

PHP Tutorial :: Date and time (III)

PHP Example #96

Select menus for hours and minutes

Samely as for months and years, select menus are used to facilitate hours and minutes. If hours are in 12 hours format, we have to include a selector for AM/PM. To make the select menu for minutes more manageable, we can use increments of five minutes instead of displaying every minute. This example shows how to do it the regular way or by using the auxiliary function input_select().

In the first example, the function printf() uses the rule %02d to add an initial 0 when minutes are less than 10; in the second example the function sprintf() is used for the same purpose and uses the same rule, but instead of printing the formatted string, the function returns it as a value ready to be assigned to a variable.

<?php
// Without using form auxiliary functions
print '<select name="hour">';
for ($hour = 1; $hour <= 12; $hour ++) {
print '<option value="' . $hour . '">' . $hour . "</option>\n";
}
print "</select>:\n";
print '<select name="minute">';
for ($minute = 0; $minute < 60; $minute += 5) {
printf('<option value ="%02d">%02d</option>', $minute, $minute);
}
print "</select> \n";
print '<select name="ampm">';
print '<option value="am">am</option>';
print '<option value="pm">pm</option>';
print '</select>';
?>

<?php
// Using form auxiliary functions
require 'scripts/formhelpers.php';
$hours = array();
for ($hour = 1; $hour <= 12; $hour ++) { $hours[$hour] = $hour; }
$minutes = array();
for ($minute = 0; $minute < 60; $minute += 5) {
$formatted_minute = sprintf('%02d', $minute);
$minutes[$formatted_minute] = $formatted_minute;
}
input_select('hour', $_POST, $hours);
print ':';
input_select('minute', $_POST, $minutes);
input_select('ampm', $_POST, array('am' => 'am', 'pm' => 'pm'));
?>
:

PHP Example #97

Processing menus for date and time

When we have different elements in a form for date and time, the function process_form() should create a timestamp value from those elements so it can be used in the program to make the necessary calculations. This example prints a form with select menus for months, days, years, hours and minutes. The function validate_form() checks that these parameters are sent with acceptable values. The function process_form() prints the date of the first meeting of the New York PHP users group after the date sent. The meetings of this group are at 6:30 p.m. in the fourth Thursday of each month. Hence, process_form() uses mktime() to calculate a timestamp from the values sent by the form, and then uses strtotime() to find the date of the meeting. If the date sent is the same day than one of the meetings, process_form() checks the time sent to inform if the meeting has already begun.

<?php
require 'scripts/formhelpers.php';
// Sets arrays of months, days, years, hours and minutes
$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');
$days = array();
for ($i = 1; $i <= 31; $i ++) { $days[$i] = $i; }
$years = array();
for ($year = date('Y') - 1, $max_year = date('Y') + 5; $year < $max_year;
$year ++) {
$years[$year] = $year;
}
$hours = array();
for ($hour = 1; $hour <= 12; $hour ++) { $hours[$hour] = $hour; }
$minutes = array();
for ($minute = 0; $minute < 60; $minute += 5) {
$formatted_minute = sprintf('%02d', $minute);
$minutes[$formatted_minute] = $formatted_minute;
}
if (! array_key_exists('_submit_check', $_POST)) {
$_POST['_submit_check'] = 0;
}
if ($_POST['_submit_check']) {
// If validate_form() returns errors, pass them to show_form()
if ($form_errors = validate_form()) {
show_form($form_errors);
} else {
// The data sent is valid, therefore, process it
process_form();
}
} else {
// The form has not been sent, then show it
show_form();
}
function show_form($errors = '') {
global $hours, $minutes, $months, $days, $years;
// If the form has been sent, obtain the default values of the variables sent
if ($_POST['_submit_check']) {
$defaults = $_POST;
} else {
// If not, set our own default values: current date and time
$defaults = array('hour' => date('g'), 'ampm' => date('a'),
'month' => date('n'), 'day' => date('j'), 'year' => date('Y'));
// Since the options in the select for minutes are in stretches of five minutes,
// if the current minute is not multiple of five, we have to convert it
$this_minute = date('i');
$minute_mod_five = $this_minute % 5;
if ($minute_mod_five != 0) { $this_minute -= $minute_mod_five; }
$defaults['minute'] = sprintf('%02d', $this_minute);
}
// If errors were passed, place them in $error_text (with HTML code)
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'] . '">';
print 'Enter a date and a time: <br/>';
input_select('hour', $defaults, $hours);
print ':';
input_select('minute', $defaults, $minutes);
input_select('ampm', $defaults, array('am' => 'am', 'pm' => 'pm'));
input_select('month', $defaults, $months);
print ' ';
input_select('day', $defaults, $days);
print ' ';
input_select('year', $defaults, $years);
print ' ';
input_submit('submit', 'Find Meeting');
print '<input type="hidden" name="_submit_check" value="1"/>';
print '</form>';
}
function validate_form() {
global $hours, $minutes, $months, $days, $years;
$errors = array();
if (! array_key_exists($_POST['month'], $months)) {
$errors[] = 'Select a valid month.';
}
if (! array_key_exists($_POST['day'], $days)) {
$errors[] = 'Select a valid day.';
}
if (! array_key_exists($_POST['year'], $years)) {
$errors[] = 'Select a valid year.';
}
if (! array_key_exists($_POST['hour'], $hours)) {
$errors[] = 'Select a valid hour.';
}
if (! array_key_exists($_POST['minute'], $minutes)) {
$errors[] = 'Select a valid minute.';
}
if (($_POST['ampm'] != 'am') && ($_POST['ampm'] != 'pm')) {
$errors = 'Select a valid am/pm choice.';
}
return $errors;
}
function process_form() {
// Before passing the form parameters to mktime(), we must convert the time
// in a 24 hours format by using $_POST['ampm']
if (($_POST['ampm'] == 'am') && ($_POST['hour'] == 12)) {
// 12 am is 0 in a 24 hours format
$_POST['hour'] = 0;
} elseif (($_POST['ampm'] == 'pm') && ($_POST['hour'] != 12)) {
// For all the pm hours except 12 pm, add 12 to the hour
// 1 pm is converted to 13, 11 pm is converted to 23, but 12 pm
// (noon) will remain as 12
$_POST['hour'] += 12;
}
// Create a timestamp value for the date facilitated by the user
$timestamp = mktime($_POST['hour'], $_POST['minute'], 0,
$_POST['month'], $_POST['day'], $_POST['year']);
// How to ascertain the next meeting of the NYPHP from the date
// facilitated by the user:
// If $timestamp is before the fourth thursday of the month, then use
// the date of the NYPHP meeting for the month in $timestamp
// If not, use the date of the NYPHP meeting for the following month
// Midnight in the date facilitated by the user
$midnight = mktime(0, 0, 0, $_POST['month'], $_POST['day'], $_POST['year']);
// Midnight of the first of the month facilitated by the user
$first_of_the_month = mktime(0, 0, 0, $_POST['month'], 1, $_POST['year']);
// Midnight of the fourth thursday of the month facilitated by the user
$month_nyphp = strtotime('fourth thursday', $first_of_the_month);
if ($midnight < $month_nyphp) {
// The date facilitated by the user is previous to the meeting day
print "NYPHP Meeting this month: ";
print date('l, F j, Y', $month_nyphp);
} elseif ($midnight == $month_nyphp) {
// The date facilitated by the user is the meeting day
print "NYPHP Meeting today. ";
$meeting_start = strtotime('6:30pm', $month_nyphp);
// If the time facilitated is after 6:30 pm, the meeting has already begun
if ($timestamp > $meeting_start) {
print "It started at 6:30 but you entered ";
print date('g:i a', $timestamp);
}
} else {
// The date facilitated by the user is posterior to the meeting day,
// therefore find the meeting day for the next month
$first_of_next_month = mktime(0, 0, 0, $_POST['month'] + 1, 1, $_POST['year']);
$next_month_nyphp = strtotime('fourth thursday', $first_of_next_month);
print "NYPHP Meeting next month: ";
print date('l, F j, Y', $next_month_nyphp);
}
}
?>
Enter a date and a time:
: