Formatting Date Strings timestamps In PHP

Most of the time computers like to work in timestamps , but in many situations you need to convert a timestamps to a string representation of a date.
Common scenarios include displaying a date in a Web page, or passing a date to another application that expects to receive a date string in a specified format.

Here are the lists of the date – related formatting characters allowed in date()’s format string:

Character Description
J This Character describes the day of the month without leading zeros.
D This Character describes the 2 – digit day of the month, with a leading zero if appropriate.
D This Character describes the day of the week as a three – letter string (such as “ Mon ” ).
l (lowercase ‘ L ’ ) This Character describes the day of the week as a full word (such as “ Monday ” ).
W This Character describes the day of the week as a number (0=Sunday, 6=Saturday).
N This Character describes the day of the week as an ISO – 8601 number (1=Monday, 7=Sunday).
S This Character describes an English ordinal suffix to append to the day of the month ( “ st, ” “ nd, ” “ rd, ” or “ th ” ). Often used with the j formatting character.
Z This Character describes the day of the year (zero represents January 1).
W This Character describes the 2 – digit ISO – 8601 week number in the year, with a leading zero if appropriate. Weeks start on Monday. The first week is week number 01.
M This Character describes the month as a three – letter string (such as “ Jan ” ).
F This Character describes the month as a full word (such as “ January ” ).
N This Character describes the month as a number (1 – 12).
M This Character describes the month as a two – digit number, with a leading zero if appropriate (01 – 12).
T This Character describes the number of days in the month (28, 29, 30, or 31).
Y This Character describes the year as a two – digit number.
Y This Character describes the year as a four – digit number.
o (lowercase “ o ” ) This Character describes the ISO – 8601 year number. This is usually the same value as Y; however if the ISO – 8601 week number belongs to the previous or the next year, that year is used instead.
L This Character describes the “1” if the date is in a leap year, “0” otherwise.

date() also allows the following time – formatting characters:

Character Description
G This Character describes the hour in 12 – hour format, without leading zeros (1 – 12).
H This Character describes the hour in 12 – hour format, with leading zeros (01 – 12).
G This Character describes the hour in 24 – hour format, without leading zeros (0 – 23).
H This Character describes the hour in 24 – hour format, with leading zeros (00 – 23).
I This Character describes the Minutes, with leading zeros (00 – 59).
S This Character describes the Seconds, with leading zeros (00 – 59).
U This Character describes the Microseconds (will always be zero because, at the time of writing, date() can only accept an integer timestamp).
B This Character describes the Swatch Internet Time — a time – zone – free, decimal time measure.
A This Character describes the “ am ” or “ pm ”, depending on the value of the hour.
A This Character describes the “ AM ” or “ PM ”, depending on the value of the hour.
E This Character describes the full time zone identifier of the currently set time zone (such as “ UTC ” or “ America/Indiana/Indianapolis ” ).
T This Character describes the time zone abbreviation for the currently set time zone (such as “ UTC ” or “ EST ” ). Abbreviations are best avoided because the same abbreviation is often used for multiple time zones throughout the world.
O (capital “ O ” ) This Character describes the time zone offset from GMT, in the format hhmm . For example, the“ America/Indiana/Indianapolis ” time zone is 5 hours behind GMT, so its offset is – 0500
P This Character describes the Same as O , but with a colon between the hours and minutes (for example, – 05:00 ).
Z This Character describes the time zone offset from GMT, in seconds. For example, the offset in seconds for the “ America/Indiana/Indianapolis ” time zone is – 18000 , because – 5 hours x 60 minutes x 60 seconds = – 18,000 seconds.
I (capital “ I ” ) This Character describes the “1” if the currently set time zone is in daylight saving time;” 0 “otherwise.

date() gives you three more formatting characters that return the date and time in one go:

Character Description
C This Character describes the date and time as an ISO 8601 date. For example, 2014 – 03 – 28 T 19:42:00+11:00 represents March 28, 2014 at 7:42 in the evening, in a time zone that is 11 hours ahead of GMT.
R This Character describes the date and time as an RFC 2822 date. For example, Tue, 28 Mar 2014 19:42:00 +1100 represents March 28, 2014 at 7:42 in the evening, in a time zone that is 11 hours ahead of GMT. RFC 2822 dates are commonly used in Internet protocols such as Web and email.
U This Character describes the timestamp that was passed to date() , or the timestamp representing the current time if no timestamp was passed.