Format Unix Timestamp To Date Time 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!

ⓘ I have included a zip file with all the source code at the start of this tutorial, so you don’t have to copy-paste everything… Or if you just want to dive straight in.

 

 

REAL QUICK SLIDES

 

TABLE OF CONTENTS

Download & Notes Unix To Date Useful Bits & Links
The End

 

DOWNLOAD & NOTES

Firstly, here is the download link to the example code as promised.

 

EXAMPLE CODE DOWNLOAD

Click here to download all the example source code, I have released it under the MIT license, so feel free to build on top of it or use it in your own project.

 

QUICK NOTES

If you spot a bug, please feel free to comment below. I try to answer questions too, but it is one person versus the entire world… If you need answers urgently, please check out my list of websites to get help with programming.

 

 

UNIX TIMESTAMP TO DATE TIME

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

 

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.

 

 

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.

 

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.

 

 

USEFUL BITS & LINKS

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

 

INFOGRAPHIC CHEAT SHEET

Format Unix Timestamp To Date Time In PHP (Click To Enlarge)

 

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 *