PHP Program to print the dates in different formats


March 2, 2022, Learn eTutorial
1363

How to print the dates in different formats using PHP?

In PHP we use the built-in function date() to get the current date. There are different characters used to change the formats.

The most commonly used characters for formating dates are:

d - It is used to get the date(form 01 to 31)

D - To get the date in letters

j - To get the date without 0 at front

l - To get the day in text

N - To get the day in numeric

z - To get the day of the year

F - To get the month in text

m - To get the month in numeric

M - To get the month in three letters

n - To get the month without 0 at front

t - To get the number of days of the month

L - To check leap year or not (1 if it is a leap year, 0 otherwise)

Y - To get the year in full four digits

y - To get the year in two digits

a - To get am or pm in lowercase

A - To get AM or PM in uppercase

g - To get time in 12-hour format of an hour (1 to 12)

G - To get time in the 24-hour format of an hour (0 to 23)

h - To get time in 12-hour format of an hour (01 to 12)

H - To get time 24-hour format of an hour (00 to 23)

i - To get minutes with leading zeros (00 to 59)

s - To get seconds, with leading zeros (00 to 59)

e - To get the timezone (Examples: UTC, GMT, Atlantic/Azores)

PHP Source Code

                                          <?php
echo date("Y/m/d") . "\n";
echo date("Y.m.d") . "\n";
echo date("Y-m-d") . "\n";
echo date("Y-M-d") . "\n";
echo date("y-M-d") . "\n";
echo date("l");
?>
                                      

OUTPUT

2021/10/15
2021.10.15
2021-10-15
2021-Oct-15
21-Oct-15
Friday