PHPGurukul

Date And Time Formatting With PHP

Hello again. In this tutorial, I will explain about the date and time Formatting. Date and time is a very important topic in Web Applications.
You may still ask yourself why I started with User Interface but take a look at this:
1202395558
Not many people can read this clearly without any problems. This is the current server time. Now see this:
2008-02-07 16:45:58
Feb 07 08 4:45:58 pm
Let’s see some examples :

echo date("d-m-y", time());           // 07-02-08
echo date("D j/n/Y", time());        // Thu 7/2/2008
echo date("jS of F Y", time());     // 7th of February 2008
echo date("d M y", time());        //07 Feb 08
echo date("l jS of F", time());   // Thursday 7th of February

Date format characters’ legend by examples:
d – Represent a day, from 01 to 31.

m – Represent a month, from 01 to 12.

y – Represent a Year , two digits.

D – Textual representation of a day,  Mon through Sun.

j – Numeric representation of a day, without leading zeros 1 through 31.

n – Numeric representation of a month, without leading zeros 1 through 12.

Y – Numeric representation of a year, four digits.

S – English ordinal suffix for the day of the month. Consist of 2 characters st, nd, rd or th.

F – Textual representation of a month, January through December.

M – Textual representation of a month, three letters Jan through Dec.

Time Formatting

echo date("G:i:s", time());      //16:45:58
echo date("H:i:s", time());     //16:45:58
echo date("g:i a.", time());   //4:45 pm.
echo date("h:i A.", time());  //04:45 PM.

G  24-hour format of an hour without leading zeros 0 through 23.

i  Numeric representation of minutes with leading zeros 00 through 59.

s  Numeric representation of seconds with leading zeros 00 through 59.

H  24-hour format of an hour with leading zeros 00 through 23.

a  Lowercase Ante meridiem and Post meridiem am or pm.

g  12-hour format of an hour without leading zeros 1 through 12.

A  Uppercase Ante meridiem and Post meridiem AM or PM.

h 12-hour format of an hour with leading zeros 01 through 12.

And when we combine both date and time formatting we get some really nice looking output strings.

echo date("l jS of F g:i A.", time());     // Thursday 7th of February 4:45 PM.
echo date("D M j G:i:s T Y", time());     // Thu Feb 7 16:45:58 EET 2008

Anuj Kumar

This is Anuj Kumar. I’m a professional web developer with 4+ year experience. I write blogs in my free time. I love to learn new technologies and share with others.
I founded PHPGurukul in September 2015. The main aim of this website to provide PHP, Jquery, MySQL, PHP Oops and other web development tutorials.
.

Recommended Tutorials for you


Add comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Follow us

Don't be shy, get in touch. We love meeting interesting people and making new friends.