SakhaliaNetHome PageHistory of the RailwayVorKutaAcceptance of cookiesAcceptance of cookies

PHP Tutorial :: Exercises (XII)

PHP Exercise #22

Printing a formatted date-time string

Use strftime() to print a date-time string that is formatted like: Today is day 20 of October and day 293 of the year 2014. The time is 07:45 PM (also known as 19:45).

<?php
$stamp = mktime(19, 45, 0, 10, 20, 2014);
print strftime('Today is day %d of %B and day %j of the year %Y. The time is %I:%M %p (also known as %H:%M) . ', $stamp);
?>
Today is day 20 of October and day 293 of the year 2014. The time is 07:45 PM (also known as 19:45) .

PHP Exercise #23

Printing a formatted date-time string

Use date() to print a date-time string that is formatted like: Today is day 20 of October and day 293 of the year 2014. The time is 07:45 PM (also known as 19:45).

<?php
$stamp = mktime(19, 45, 0, 10, 20, 2014);
print 'Today is day ' . date('d', $stamp) . ' of ' . date('F', $stamp) . ' and day ' . (date('z', $stamp) + 1);
print ' of the year ' . date('Y', $stamp) . ' . The time is ' . date('h:i:A', $stamp);
print ' (also known as ' . date('H:i', $stamp) . ').';
?>
Today is day 20 of October and day 293 of the year 2014 . The time is 07:45:PM (also known as 19:45).

PHP Exercise #24

Finding certain future dates

The Labor Day Party in United States is the first Monday of September. Write a program that prints a table of the dates that the Labor Day Party will be celebrated from 2014 to 2030.

<?php
print '<table>';
print '<tr><th>Year</th><th>Labor Day</th></tr>';
for ($year = 2014; $year <= 2030; $year ++) {
// Obtains the timestamp value for the 1st of September of $year
$stamp = mktime(12, 0, 0, 9, 1, $year);
// Advances to the first Monday
$stamp = strtotime('monday', $stamp);
print "<tr><td>$year</td><td>";
print date('F j', $stamp);
print "</td></tr>\n";
}
print '</table>';
?>
YearLabor Day
2014September 1
2015September 7
2016September 5
2017September 4
2018September 3
2019September 2
2020September 7
2021September 6
2022September 5
2023September 4
2024September 2
2025September 1
2026September 7
2027September 6
2028September 4
2029September 3
2030September 2

PHP Exercise #25

Finding certain future dates

Write a program that shows a form on which users can select a day, a month and a year of the future, and then prints a list of every Tuesday between the current date and the date submitted by the user.

<?php
require 'scripts/formhelpers.php';
// Sets arrays for 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;
}
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, then process it
process_form();
}
} else {
// The form has not been sent, then show it
show_form();
}
function show_form($errors = '') {
global $months, $days, $years;
// If the form is sent, obtain the default values of the variables sent
if ($_POST['_submit_check']) {
$defaults = $_POST;
} else {
// If not, set our own default values: a month from now onwards
$default_timestamp = strtotime('+1 month');
$defaults = array('month' => date('n', $default_timestamp),
'day' => date('j', $default_timestamp),
'year' => date('Y', $default_timestamp));
}
print '<form method="post" action="' . $_SERVER['PHP_SELF'] . '">';
print 'Enter a date and a time:';
input_select('month', $defaults, $months);
print ' ';
input_select('day', $defaults, $days);
print ' ';
input_select('year', $defaults, $years);
print ' ';
input_submit('submit', 'Find Tuesdays');
print '<input type="hidden" name="_submit_check" value="1"/>';
print '</form>';
}
function validate_form() {
global $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.';
}
// Ensures that the date sent is posterior
// Finds timestamp value for today's midnight
$midnight = mktime(0, 0, 0);
// Finds timestamp value for the midnight of the date sent
$midnight_submitted = mktime(0, 0, 0, $_POST['month'], $_POST['day'],
$_POST['year']);
if ($midnight_submitted <= $midnight) {
$errors[] = 'Enter a date in the future.';
}
return $errors;
}
function process_form() {
// Creates a timestamp value for the date facilitated by the user
$midnight_submitted = mktime(0, 0, 0, $_POST['month'], $_POST['day'],
$_POST['year']);
// Obtains a timestamp value for the next Tuesday (included today
// if it is Tuesday
$timestamp = strtotime('tuesday');
if ($timestamp >= $midnight_submitted) {
print 'There are no Tuesdays between ';
print date('l, F j, Y');
print ' and ';
print date('l, F j, Y.', $midnight_submitted);
} else {
while ($timestamp < $midnight_submitted) {
// Prints a formatted date string for $timestamp (which is a Tuesday)
print date('l, F j, Y', $timestamp);
print '<br/>';
// Adds a week to $timestamp
$timestamp = strtotime(' + 1 week', $timestamp);
}
}
}
?>
Enter a date and a time: