How to convert a Unix timestamp to DateTime

Since I always try to find what I need instead of how to get there, I don’t use the lingo from the title, like “how to do anything”. In case of the current post, I create a datetime from Unix timestamp, or, in other words, a datetime converted from Unix timestamp.

How to create a datetime object from Unix timestamp

First off, Unix timestamp is a number of seconds since January, 1st, 1970, UTC. If you’ve skimmed through that date, mind that it contains a timezone, and it is UTC. Thus, there is no such thing as Unix timestamp in CET or Los-Angeles timezone. There is always the single Unix timestamp. It’s like the single moment in absolute timescale, but it just happens so that different countries have different local time.

To bring my point home, consider a timestamp which equals to 1504534440. If you wonder what time it is in UTC, here you go:

(new FromTimestamp(1504534440))->value(); // returns 2017-09-04T14:14:00+00:00

At the same moment in time, most people in Kalinigrad already have had a dinner:

(new AdjustedAccordingToTimeZone(
    new FromTimestamp(1504534440),
    new Kaliningrad()
))
    ->value(); // it's 2017-09-04T16:14:00+02:00

And still at the same moment in time, it’s an early morning in Honolulu:

(new AdjustedAccordingToTimeZone(
    new FromTimestamp(1504534440),
    new HawaiiWithNoDST()
))
    ->value(); // 2017-09-04T04:14:00-10:00

After you’ve got an ISO8601DateTime object, you can do some more: subtract seconds, minutes, hours, days, months, years from it, calculate a difference between datetimes, format it anyway you like, and much more. Consider quick start entry for more info.

And the other way round

What if you need to convert a datetime object into Unix timestamp? Since what you need is a number of seconds since January, 1st, 1970 UTC, there is a special class for that: SecondsSinceJanuary1st1970. Here is how it’s invoked:

(new SecondsSinceJanuary1st1970(
    new FromISO8601('2014-11-21T06:04:31+00:00')
))
    ->value(); // returns 1416549871

If you wonder why I have so many classes, check out my philosophy.