Extracting Date and Time Values from a Timestamps In PHP

Now you know how to create timestamps from time/date values and strings. You can also go the other way, and convert a timestamp to its corresponding date and time components.

getdate() accepts a timestamp and returns an associative array of date/time values corresponding to the supplied timestamp. The array contains the following keys:

Array Key Description Possible Values
seconds This Key describes the seconds component 0 to 59
minutes This Key describes the minutes component 0 to 59
hours This Key describes the hours component, in 24 – hour format 0 to 23
mday This Key describes the day of the month 1 to 31
wday This Key describes the day of the week as a number 0 (Sunday) to 6 (Saturday)
Mon This Key describes the month component as a number 1 to 12
Year This Key describes the year component as a four – digit number Typically 1970 to 2038
Yday This Key describes the day of the year 0 to 365
weekday This Key describes the day of the week as a string Sunday to Saturday
Month This Key describes the month component as a string January to December
0 (zero) This Key describes the timestamp Typically – 2147483648 to 2147483647

If you just want to extract a single date or time component from a timestamp, you can use idate() . This function accepts two parameters: a format string and an optional timestamp. The single – character format string dictates the component to return, and the format in which to return it, as follows:

Format String Description
B This Format string describes the Swatch Internet Time — a time – zone – free, decimal time measure.
D This Format string describes the Day of the month.
H This Format string describes the Hours (in 12 – hour format).
H This Format string describes the Hours (in 24 – hour format).
I This Format string describes the Minutes.
I This Format string describes the “1” if DST (Daylight Saving Time) is in effect, “0” otherwise.
L This Format string describes the “1” if the date is in a leap year, “0” otherwise
M This Format string describes the Month number (1 – 12).
S This Format string describes the Seconds.
T This Format string describes the number of days in the month (28, 29, 30, or 31).
U This Format string describes the timestamp.
W This Format string describes the day of the week, as a number (0 is Sunday).
W This Format string describes the week number within the year (counting from 1).
Y This Format string describes the year as a two – digit number.
Y This Format string describes the year as a four – digit number.
Z This Format string describes the day number within the year (0 is January 1).
Z This Format string describes the offset of the computer’s time zone from UTC (in seconds).