php

PHP date() Function

The date and time values are calculated in the computer by using a timestamp that reads the time passed in the number of seconds based on the UNIX epoch (starts from January 1, 1970, 00:00:00 GMT). The date() function is a built-in PHP function to convert the timestamp value into a human-readable date and time format. The syntax of this function is given below.

Syntax:

string date(string $format [, int $timestamp = null])

The first argument is mandatory, and it is used to define the format that will be used to convert the current timestamp or the particular timestamp value into a human-readable format.

The second argument is optional, and it is used to define the particular timestamp value that will be converted based on the format string. The default value of this argument is null.

It returns the formatted output of the timestamp value.

Formatting Characters

Many types of characters are used to format the date value. Some commonly used formatting characters are given below.

Character Purpose
D It is used to display the day of the week in short form. (Mon to Sun)
M It is used to display the month’s name in short form. (Jan to Dec)
Y It is used to display the four-digit year.
d It is used to display the day of the month with the leading zero. (01 to 31)
m It is used to display the number of months with the leading zero. (01 to 12)
j It is used to display the day of the month without the leading zero. (1 to 31)
n It is used to display the number of months without the leading zero. (1 to 12)
Y It is used to display the two-digit year.
F It is used to display the full month name. (January to December)
l It is used to display the full weekday name. (Monday to Sunday)
S It is used to display the day of the month with the ordinal number. (1st, 2nd, 3rd, 4th …)

date() Function Examples

The date() function uses have been shown in the next part of this tutorial by using multiple examples.

Example-1: Print the Current Date With the Format

Create a PHP file with the following script that print the formatted current date by using date() function. The format string, “jS F, Y” has been used to format the date. Here, ‘jS’ has used to print the day of the month with ordinal number, ‘F’ has used to print the month name in short form, and ‘Y’ has used to print the four-digits year value.

<?php

//Read the current date with the formatting string

$today = date("jS F, Y");

//Print the formatted date

echo "<br/><center> <h2>Today is $today</h2> </center>";

?>

Output:

The following output will appear after executing the above script.

Example-2: Print the Current Date and Time With the Format

Create a PHP file with the following script to print the current date and time in the human-readable format using the date() function. The first format string used in the date() function will print the date by separating the day, month, and year values with the hyphen(-). The second format string used in the date() function will print the time by separating the hour, minute, and second values with the colon(:) and meridiem.

<?php

//Read the current date and time with the formatting string

$dt = date("d-M-Y");

$tm = date("h:i:s A");

//Print the formatted date and time

echo "<center> <h2>Today is $dt</h2> </center>";

echo "<center> <h2>The current time is $tm</h2> </center>";

?>

Output:

The following output will appear after executing the above script.

Example-3: Print the Particular Date by Using the mktime() Function

The mktime() function generates a timestamp value based on the 6 argument values of this function. The first three arguments of this function take the hour, minute, and second values. The last three arguments of this function take the month, day, and year values. Create a PHP file with the following script that will generate a formatted date based on the output of the mktime() function.

<?php

//Read the particular date value by using mktime() function

$dt = date('l, jS F, Y', mktime(0, 0, 0, 5, 10, 2022));

//Print the date value

echo "<center><h2>The date is $dt.</h2></center>";

?>

Output:

The following output will appear after executing the above script.

Example-4: Print the Future Date by Using mktime() Function

Create a PHP file with the following script that will generate a future date with the formatting based on the output of the mktime() function. The future date will be generated by adding 5 months and 10 days with the current date.

<?php

//Read the timestamp value of the future date

$tm = mktime(0, 0, 0, date("m")+5, date("d")+10, date("Y"));

//Read the formatted date based on the timestamp value

$dt = date('d-m-Y', $tm);

//Print the date value

echo "<center><h2>The date is $dt.</h2></center>";

?>

Output:

The following output will appear after executing the above script.

Example-5: Print the Future and Previous Date by Using the strtotime() Function

Create a PHP file with the following script that will generate a future date and the previous date with the formatting based on the output of the mktime() function. The previous date will be generated by deducting 2 months and 35 days from the current date. The future date will be generated by adding 3 months and 5 days with the current date.

<?php

//Print the current date

echo '<center><h2>Today is '.date("jS F, Y").'</h2></center>';

//Read the timestamp of the previous date

$prev_timestamp = strtotime("2 months 35 days ago");

//Print the previous date based on the timestamp

echo '<center><h2>The previous date is '.date("jS F, Y", $prev_timestamp).'</h2></center>';

//Read the timestamp of the next date

$next_timestamp = strtotime("+3 months +5 days");

//Print the future date based on the timestamp

echo '<center><h2>The future date is '.date("jS F, Y", $next_timestamp).'</h2></center>';

?>

Output:

The following output will appear after executing the above script.

Conclusion

Different ways of using the date() function for different purposes have been shown in the examples of this tutorial to help PHP users to know the use of this function properly.

About the author

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.