Extracting Dates From the Unix Timestamp
In my work, I frequently deal with the Unix timestamp. It is a number that represents how many seconds have elapsed since midnight, January 1, 1970 UTC. It was established with the Unix operating system when the developers wanted a simple and consistent way to represent time.
The timestamp is frequently found in database and log files.
Currently, the timestamp is a 10-digit number. The last 9-digit timestamp occurred September 9, 2001 at 1:46:39 UTC. The 11-digit timestamp won't happen until November of the year 2286.
Here is an example.
1734438615
The beauties of the Unix timestamp are that (i) it requires very little room when stored in files and (ii) it can be used to determine the represented time for any time zone on Earth.
Of course, I made a tool to extract the date and time from the timestamp. You'll find it at the Date and Time tools page.
It is fairly simple code if you wish to do it yourself, instead of using the tool linked to above. This line of PHP code will do the trick.
echo date('r',1234567890);
/* Assuming 1234567890 is the timestamp. */
The 'r'
format specification results in a human-readable datetime. (Technically, it is an RFC 5322 formatted date.) The date is formatted in this way:
Fri, 13 Feb 2009 23:31:30 +0000
Other formats can be specified. I like 'r'
because it is succinct and the format parameter is easy to remember. See the DateTime Formats page for a list of characters that can be used when specifying your preferred format.
Notice that the formatted datetime example, above, ends with +0000
. That means the datetime was calculated for UTC. The number indicates the plus or minus offset for the time zone the datetime was calculated for. A number other than +0000
would indicate a different time zone.
As stated further above, the code in this article is provided so you can make your own if you wish to do so.
Whether or not you make your own, you are welcome to use the Date and Time tools page to extract the datetimes from Unix timestamps. (The tools page also has a reverse converter, date/time to timestamp.)
(This content first appeared in Possibilities newsletter.)
Will Bontrager