Convert Unix Timestamp To Date In PHP (Simple Examples)

Welcome to a quick tutorial on how to format a Unix Timestamp to a readable date and time in PHP. Need to get a “human-friendly” date from a Unix Timestamp?

We can use the date function to format a Unix Timestamp in PHP.

  • To the ISO 8601 format – $formatted = date(DateTimeInterface::ATOM, $UNIX);
  • To MySQL date format – $formatted = date("Y-m-d H:i:s", $UNIX);
  • Various other custom formats – $formatted = date("D, j F Y h:i:s A", $UNIX);

That covers the basics, but read on for more examples!

 

 

TABLE OF CONTENTS

 

DOWNLOAD & NOTES

Here is the download link to the example code, so you don’t have to copy-paste everything.

 

EXAMPLE CODE DOWNLOAD

Source code on GitHub Gist

Just click on “download zip” or do a git clone. I have released it under the MIT license, so feel free to build on top of it or use it in your own project.

 

SORRY FOR THE ADS...

But someone has to pay the bills, and sponsors are paying for it. I insist on not turning Code Boxx into a "paid scripts" business, and I don't "block people with Adblock". Every little bit of support helps.

Buy Me A Coffee Code Boxx eBooks

 

 

PHP UNIX TIMESTAMP TO DATE TIME

All right, let us now get into the examples of formatting a Unix Timestamp in PHP.

 

EXAMPLE 1) PREDEFINED DATE FORMATS

1-predefined.php
<?php
// (A) 07 AUG 1999 05:04:03 UTC
$unix = 934002243;

// (B) ISO 8601
$iso = date(DateTimeInterface::ATOM, $unix);
echo $iso . "<br>";

// (C) COOKIE
$cookie = date(DateTimeInterface::COOKIE, $unix);
echo $cookie . "<br>";

// (D) EMAIL (RFC 2822)
$mail = date(DateTimeInterface::RFC2822, $unix);
echo $mail . "<br>";

// (E) HTTP 1.1 (RFC 7231)
$http = date(DateTimeInterface::RFC7231, $unix);
echo $http . "<br>";

As in the introduction, the almighty date() function is pretty much all we need. PHP also comes with a list of predefined Internation standard date-time formats. If you need a full list of the predefined formats, I will leave a link to the PHP manual in the extras section below.

 

 

EXAMPLE 2) CUSTOM DATE FORMATS

2-custom.php
<?php
// (A) 07 AUG 1999 05:04:03 UTC
$unix = 934002243;

// (B) MYSQL
$mysql = date("Y-m-d H:i:s", $unix);
echo $mysql . "<br>";

// (C) CUSTOM DATE FORMAT
$customA = date("d/m/y H:i:s", $unix);
echo $customA . "<br>";

$customB = date("D, j F Y h:i:s A", $unix);
echo $customB . "<br>";

Apart from taking in predefined formats, we can also fully customize our own date-time formats. There is a long list of “date-time parts” we can use, but I will not copy here… Again, I shall leave a link to the PHP manual below.

 

EXAMPLE 3) SETTING THE TIMEZONE

3-timezone.php
<?php
// (A) 07 AUG 1999 05:04:03 UTC
$unix = 934002243;

// (B) DATE WILL DEFAULT TO UTC 
$tzA = date("Y-m-d H:i:s e", $unix);
echo $tzA . "<br>";

// (C) SET TIMEZONE
$tzB = new DateTime();
$tzB->setTimestamp($unix);
$tzB->setTimezone(new DateTimeZone("America/New_York"));
echo $tzB->format("Y-m-d H:i:s e") . "<br>";
$tzB->setTimezone(new DateTimeZone("Asia/Tokyo"));
echo $tzB->format("Y-m-d H:i:s e") . "<br>";

Finally, setting the timezone is a little confusing. By default, the date() function will assume UTC+0 when parsing a Unix Timestamp. To set/change the timezone, we have to do it the “long-winded” way.

  • Create a $tz = new DateTime() object.
  • Then set it to the Unix Timestamp – $tz->setTimestamp($UNIX).
  • At this point, we can do a $formatted = $tz->format("FORMAT") to create a date string. But this defaults to UTC+0 again.
  • To change the timezone, we simply do a $tz->setTimezone(TIMEZONE).

I shall leave a Wikipedia link below to the full list of international timezones.

 

 

EXTRAS

That’s all for the tutorial, and here is a small section on some extras and links that may be useful to you.

 

LINKS & REFERENCES

 

THE END

Thank you for reading, and we have come to the end. I hope that it has helped you to better understand, and if you want to share anything with this guide, please feel free to comment below. Good luck and happy coding!

Leave a Comment

Your email address will not be published. Required fields are marked *