Today, In this article i will explain date various format in PHP and how to use it in Php according to our requirement in Programming functionality.
In Php, date() function accepts two parameters. That are Format string and time as integer.
Let’s play with Date() function by examples.
Date formatting :
echo date("d-m-y", time()); // 04-12-13 //Here We use time() for current time integer. //The dash between the characters in the format string is exactly what the output is going to be separated with. Like in below example – replaced with / and out date getted with / separated. echo date("D j/n/Y", time()); // Wed 4/12/2013 echo date("jS of F Y", time()); // 4th of December 2013 //We can use any character we want between date, month and yeas as seprator. echo date("d M y", time()); //04 Dec 13 echo date("l jS of F", time()); // Wednesday 4th of December
Below are list of Date format characters:
d – Numeric representation of a day, with leading zeros 01 through 31.
m – Numeric representation of a month, with leading zeros 01 through 12.
y – Numeric representation of a year, two digits.
D – Textual representation of a day, three letters 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 like 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.
l – textual representation of the day of the week Sunday through Saturday.
Time formatting :
//1 time format in 24 hour without leading zeros 0 to 23 echo date("G:i:s", time()); //20:50:55 //2 time format in 24 hour with leading zeros 00 to 23 echo date("H:i:s", time()); //16:45:58 //3 time format with lowercase AM/PM. echo date("g:i a.", time()); //4:45 pm. //4 time format with uppercase AM/PM. echo date("h:i A.", time()); //04:45 PM.
Below are list of Time format characters:
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.
Example of combine both date and time format.
echo date("l jS of F g:i A.", time()); // Wednesday 4th of December 1:10 PM. echo date("d m Y H:i a.", time()); // 04 12 2013 23:05 pm. echo date("D M j G:i:s T Y", time()); // Wed Dec 4 20:15:50 CET 2013 echo date("jS F Y G:i:s", time()); // 4th December 2013 4:20:01 echo date("D j/n/Y h:i:s A", time()); // Wed 3/12/2013 01:05:09 PM