php

Use of Date and Time in PHP

Different types of date and time-related operations are required to do on the website frequently. UNIX timestamp value is used to store date and time in the computer. The timestamp value is calculated by counting the number of seconds passed from the date and time, January 1, 1970, 00:00:00 GMT. But the timestamp value is not human-readable. PHP has many built-in date and time-related functions to read the date and time values from the timestamp value. How to work with date and time values in PHP is shown in this tutorial.

Use of PHP date() function

One of the most commonly used functions of PHP for doing date and time-related tasks is the date(). The syntax of this function is given below.

Syntax:
date ( string $format [, int $timestamp = time() ] ) : string

Here, the first parameter is mandatory, and it is used to define the date or time or its format to display the output. The second parameter is optional, and it is used to specify the particular timestamp value to print the date and time value based on that timestamp value. If the second argument is omitted, then the date() function will generate the output based on the system date and time. It returns the date and time of the current system date in string format.

Format Characters:

Different format characters are used for the date() function to format the date and time. Some commonly used format characters with explanations are given below.

Used For Character Purpose Example
Date


D Display the weekday in short form. Mon to Sun
d Display the day of the month with leading zero. 01 to 31
M Display the month name in short form. Jan to Dec
m Display the month as a number with leading zero. 01 to 12
Y Display four digits year. 2020
y Display two digits year. 20
F Display the month’s name in full form. January to December
l Display the weekday name in full form. Monday to Sunday
n Display the month as a number without leading zero. 1 to 12
j Display the day of the month without leading zero. 1 to 31
S Display 1st, 2nd, 3rd, etc. with the day of the month. 1st, 2nd, 3rd and 4th to 30th

Time

G Display hour in 24-hour format without leading zero. 1 to 24
g Display hour in 12-hour format without leading zero. 1 to 12
H Display hour in 24-hour format with leading zero. 01 to 24
h Display hour in 12-hour format with leading zero. 01 to 12
A Display am/pm in capital letter. AM/PM
a Display am/pm in a small letter. am/pm
i Display minutes with leading zero. 01 to 60
s Display seconds with leading zero. 01 to 60

Example-1: Use of date() function

Different uses of the date() function are explained in this example. The first date() function will return the current system date in a short form. The second date() function will return the current system time. The third date() function will return the date and time based on the timestamp value. The strtotime() function will return the timestamp value based on the value return by the fourth date() function.

<?php
//Display the current date
echo "Today is ".date('d-M-Y')."<br />";
//Display the current time
echo "The current time is ".date('h:m:i A')."<br />";
//Display the date based on the timestamp value
echo "The date is ".date('l, jS F Y',1476721600)."<br />";
//Display the timestamp of the current date
echo "The timestamp value of current date is ".strtotime(date('d-m-y'));
?>

Output:
The following output will appear after running the script.

Use of PHP time() function

It is used to get the timestamp value of the current date. The format characters of time that are mentioned above for the date() function can be used to format the value of time() function. The syntax of this function is given below.

Syntax:
time ( void ) : int

It has no parameter and it returns an integer value as a timestamp.

Example-2: Use of time() function

In the following script, the first time() function will return the timestamp value of the system date and time, as the second parameter of the date() function will return the formatted date. In the same way, the second date() function will return the time value based on the return value of the second time() function. How the time() function can be used to find out the difference between two timestamp values is shown in the next part of this script. Two timestamp values are stored in two variables with 5 seconds delay.

<?php

//Print the date on timestamp generated by time() function
echo "<p style='font-size:25px'>Today is <b>".date("l, F d, Y", time())."</b> </p>";
//Print the time on timestamp generated by time() function
echo "<p style='font-size:25px'>The current time is <b>".date('h:i:s A',time())."</b> </p>";
//Set start time
$start_time = time();
//Delay for 5 seconds
sleep(5);
//Set end time
$end_time = time();
//Print the time differences
echo "<p style='font-size:25px'>
   Time difference is <b>"
.($end_time - $start_time)." seconds</b> </p>";
?>

Output:
The following output will appear after running the script. The ‘l, F d, Y’ is used as format characters in the first date() function of the script. The full weekday name is printed for ‘l’ character, the full month name printed for ‘F’ character, the day of the month is printed for ‘d’ character, and the full-year value is printed for ‘Y’ character. Next, ‘h:i:s A‘ is used as format characters in the second date() function to print hour, minute, and second values with AM/PM. The sleep() function is called with 5 seconds delay here. So, the third output shows that the time difference between the two timestamp values is 5 seconds.

Conclusion:

The use of date and time functions are very common requirements for any web application. How different format characters can be used to print date and time are explained in this tutorial by using different examples to help the PHP users to do date and time-related operations easily in their scripts.

About the author

Fahmida Yesmin

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.