How to Format and Display the Date in PHP


In this tutorial, we go over how to format and display dates in PHP.

PHP offers a great deal of dynamic ability to display dates in pretty any format we would like to. We can display the day alone, the month alone, the year alone, or any combination of these 3. We can display the year with the last 2 numbers such as 13 or we can display the full 4 numbers, 2013. We can show dates separated by slashes (ex. 1/12/2012) or show dates separated by dots (1.12.2012).

PHP offers a wide range of options.

To format and display the date in PHP, the date() function is used.

The date() function uses 4 characters to help denote the date, shown below:

Character Description
Y A four-digit year such as 2012
y A two-digit year such as 12
m Displays the month with leading zeroes (01)(for january)
d Dispalys the day with leading zeroes (09)(for the 9th of the month)

Formatting the Date

$date= date('Y-m-d'); // 2012-02-10

$date= date('m/d/y'); // 02/10/12
$date= date('d.m.Y'); // 10.02.2012

$date= date ('Y'); // 2012

Displaying the Date

Once you have formatted the date above and assigned it to a variable, you then display the date by using the echo function to display the date.

$date= date('Y-m-d');
echo $date; // 2012-02-10

$date= date('m/d/y');
echo $date; // 02/10/12

$date= date('d.m.Y');
echo $date;// 10.02.2012

$date= date ('Y');
echo $date; //2012

Actual PHP Output


Note- PHP's date() function is very useful and used because it displays the real-time date. This isn't a function used to display past dates. For past static dates which don't change, you can just manually type in the date with plain HTML. PHP's date() function is used because it always provides the current real-time date. This is why you see the today's current date above below the Actual PHP Output. Javascript has a similar function which allows real-time current dates to be shown as well.

HTML Comment Box is loading comments...