SakhaliaNetHome PageHistory of the RailwayVorKutaAcceptance of cookiesAcceptance of cookies

PHP Tutorial :: Date and time (I)

PHP Example #86

Showing date and time

Dates and time are constantly found in web applications. They are used to mark important events in the time, such as when an order was made, when a forum post was published or when an user was connected for the last time. Dates and time are harder to manage than strings and numbers, for they are collections of different values, and making operations with them is particularly complex. A convention used in the world of information technology for simplifying the calculation of date and time is the timestamp, which counts the number of seconds lapsed since 1 January 1970.

The most simple way of working with date and time is showing an user the current time, by using functions like date() or strftime().

<?php
print 'date() says: ';
print date('r');
print '<br/>';
print 'strftime() says: ';
print strftime('%c');
?>
date() says: Sun, 24 Sep 2017 08:18:45 -0400
strftime() says: Sun Sep 24 08:18:45 2017

PHP Example #87

Showing date and time

Both date() and strftime() have two parameters. The first parameter controls the format of the date and time to display, and the second one controls the date or time to use; if this parameter is not passed, it takes by default the current time. With date(), the individual letters in the format string are translated in certain datetime values, as seen below, where "m" is the month, "d" is the day and "y" is the year. since the slash is not an actionable character for date(), it is left as it is in the returned string. In strftime() the parameters work similarly but including the percentage symbol.

<?php
print 'date() says: ';
print date('m/d/y');
print '<br/>';
print 'strftime() says: ';
print strftime('%m/%d/%y');
?>
date() says: 09/24/17
strftime() says: 09/24/17

PHP Example #88

Showing date and time

This example prints the time that would be one hour after current time. The function time() returns the current time and we add 3600 (60 * 60) seconds to that time. Note that the value returned corresponds to the timezone of the server where the page is hosted.

<?php
print 'date() says: ';
print date('h:i:s', time() + (60 * 60));
print '<br/>';
print 'strftime() says: ';
print strftime('%I:%M:%S', time() + (60 * 60));
?>
date() says: 09:18:45
strftime() says: 09:18:45

PHP Example #89

Time strings and text

The function strftime() is better suited than date() for combining time strings with normal strings. However, date() possesses some unique features, such as indicators for leap years and Daylight Saving Time, and is specific of PHP, while strftime() is dependant on the namesake function that is underlying in the operative system. for such reason date() is usually recommended over strftime().

<?php
print 'date() says: ';
print 'Today is ' . date('m/d/y') . ' and the time is ' . date('h:i:s');
print '<br/>';
print 'strftime() says: ';
print strftime('Today is %m/%d/%y and the time is %I:%M:%S');
?>
date() says: Today is 09/24/17 and the time is 08:18:45
strftime() says: Today is 09/24/17 and the time is 08:18:45

PHP Example #90

Analyzing date and time

To convert different representations of date and time values to timestamp values for using in a program, we can use the function mktime(), which accepts hours, minutes, second, months, days and years and returns the corresponding timestamp. All the arguments in mktime() are optional; anything that is not passed, will take by default the current value. The program in this example simulates to take input from a form and passes the values to mktime() to calculate the number of seconds elapsed since 1st January 1970.

<?php
$_POST['hour'] = 16;
$_POST['minute'] = 20;
$_POST['month'] = 1;
$_POST['day'] = 31;
$_POST['year'] = 2014;
$user_date = mktime($_POST['hour'], $_POST['minute'], 0,
$_POST['month'], $_POST['day'], $_POST['year']);
print strftime('At %I:%M:%S on %m/%d/%y, ', $user_date);
print "$user_date seconds have elapsed since 1/1/1970.";
?>
At 04:20:00 on 01/31/14, 1391203200 seconds have elapsed since 1/1/1970.

PHP Example #91

Analyzing date and time

The function strtotime() is useful for retrieving the timestamp values of relative time values. In this example we use it to find the date of the previous and the next Thursday from the current time.

<?php
$now = time();
$later = strtotime('Thursday', $now);
$before = strtotime('last thursday', $now);
print strftime('Now: %c <br/>', $now);
print strftime('Later: %c <br/>', $later);
print strftime('Before: %c <br/>', $before);
?>
Now: Sun Sep 24 08:18:45 2017
Later: Thu Sep 28 00:00:00 2017
Before: Thu Sep 21 00:00:00 2017

PHP Example #92

Analyzing date and time

The function strtotime() also accepts a second parameter of timestamp value to use it as reference for its calculations. This example uses mktime() and strtotime() to find when will be the US elections in 2016, knowing that they are celebrated the Tuesday after the first Monday in November.

<?php
// Finds the timestamp value for the 1st November 2016
$november = mktime(0,0,0,11,1,2016);
// Finds the first Monday on or after the 1st November 2016
$monday = strtotime('Monday', $november);
// Advances one day till the Tuesday after the first Monday
$election_day = strtotime('+1day', $monday);
print strftime('Election day is %A, %B %d, %Y', $election_day);
?>
Election day is Tuesday, November 08, 2016