SakhaliaNetHome PageSahara TerritoryVorKutaAcceptance of cookiesAcceptance of cookies

PHP Tutorial :: Date and time (II)

PHP Example #93

Date and time selection in forms

When we need users to be able to input date-time values in a form the best method is by using select menus, which will restrict the possible values to the ones that we want. This example prints a select menu that lists the days of the week that starts in the current day, and it shows how to do it the regular way or by using the auxiliary function input_select().

<?php
// Without using form auxiliary functions
$midnight_today = mktime(0,0,0);
print '<select name="date">';
for ($i = 0; $i < 7; $i ++) {
$timestamp = strtotime("+$i day", $midnight_today);
$display_date = strftime('%A, %B %d, %Y', $timestamp);
print '<option value="' . $timestamp . '">' . $display_date . "</option>\n";
}
print "\n</select>";
?>

<?php
// Using form auxiliary functions
require 'scripts/formhelpers.php';
$midnight_today = mktime(0,0,0);
$choices = array();
for ($i = 0; $i < 7; $i ++) {
$timestamp = strtotime("+$i day", $midnight_today);
$display_date = strftime('%A, %B %d, %Y', $timestamp);
$choices[$timestamp] = $display_date;
}
input_select('date', $_POST, $choices);
?>

PHP Example #94

Date and time selection in forms

To allow users to input any date we have to facilitate different select menus for months, days and years. This example shows how to do it the regular way or by using the auxiliary function input_select().

<?php
// Without using form auxiliary functions
$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');
print '<select name="month">';
// An option for each element in $months
foreach ($months as $num => $month_name) {
print '<option value="' . $num . '">' . $month_name ."</option>\n";
}
print "</select>\n";
print '<select name="day">';
// An option for each day from the 1st to the 31th
for ($i = 1; $i <= 31; $i ++) {
print '<option value="' . $i . '">' . $i . "</option>\n";
}
print "</select>\n";
print '<select name="year">';
// An option for each year from the last year to the next
// five from now onwards
for ($year = date('Y') -1, $max_year = date('Y') + 5; $year < $max_year;
$year ++) {
print '<option value="' . $year . '">' . $year . "</option>\n";
}
print "</select>\n";
?>

<?php
// Using form auxiliary functions
require 'scripts/formhelpers.php';
$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;
}
input_select('month', $_POST, $months);
print ' ';
input_select('day', $_POST, $days);
print ' ';
input_select('year', $_POST, $years);
?>

PHP Example #95

Checking expiration date

A common application for date input is checking the expiration date of credit cards. This example shows select menus for month and year to provide the expiration date. The program checks if the month and the year provided are previous to the current month and year; if it is so, the credit card has expired.

<?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'), $max_year = date('Y') + 10; $year < $max_year;
$year ++) {
$years[$year] = $year;
}
if (! array_key_exists('_submit_check', $_POST)) {
$_POST['_submit_check'] = 0;
}
if ($_POST['_submit_check']) {
if ($form_errors = validate_form()) {
show_form($form_errors);
} else {
process_form();
}
} else {
show_form();
}
function show_form($errors = '') {
if ($errors) {
print 'Please correct the following errors: <ul><li>';
print implode('</li><li>', $errors);
print '</li></ul>';
}
print '<form method="post" action="' . $_SERVER['PHP_SELF'] . '">';
print 'Expiration Date: ';
input_select('month', $_POST, $GLOBALS['months']);
print ' ';
input_select('year', $_POST, $GLOBALS['years']);
print ' ';
input_submit('submit', 'Check Expiration');
print '<input type="hidden" name="_submit_check" value="1"/>';
print '</form>';
}
function validate_form() {
$errors = array();
// Asegurarse de que se han facilitado un mes y un año válidos
if (! array_key_exists($_POST['month'], $GLOBALS['months'])) {
$errors[] = 'Please select a valid month.';
}
if (! array_key_exists($_POST['year'], $GLOBALS['years'])) {
$errors[] = 'Please select a valid year.';
}
// Asegurarse de que el mes y el año son el mes
// y el año en curso o posterior
$this_month = date('n');
$this_year = date('Y');
if ($_POST['year'] < $this_year) {
// Si el año facilitado pertenece al pasado, la tarjeta de
// crédito está caducada
$errors[] = 'The credit card is expired.';
} elseif (($_POST['year'] == $this_year) &&
($_POST['month'] < $this_month)) {
// Si el año facilitado es este año y el mes facilitado es
// anterior a este mes, entonces la tarjeta de crédito está
// caducada
$errors[] = 'The credit card is expired.';
}
return $errors;
}
function process_form() {
print "You entered a valid expiration date.";
}
?>
Expiration Date: