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 :
1 2 3 4 5 |
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
1 2 3 4 |
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.
1 2 |
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 |