PHP

Date And Time Formatting With PHP

Hello again.In this tutorial I will explains about date and time Formatting . Date and and time is 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

Recommended Tutorials for you


Related posts

PHP – Data Types

Anuj Kumar

PHP Variables

Anuj Kumar

How to send Mail Using PHP

Anuj Kumar

Time Ago Script

Anuj Kumar

Server Side form Validation in PHP

Anuj Kumar

PHP Constants

Anuj Kumar

How to Insert Data in Database using PHP OOPS Concept

Anuj Kumar

Conditional Statement

Anuj Kumar

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

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