Skip to content
Convert PHP Timestamp to Date Format

Convert PHP Timestamp to Date Format

PHP Code Snippets > Convert PHP Timestamp to Date Format

by Steve Dawson Web Developer


strong>How to convert a PHP Timestamp to a readable date format

PHP Timestamps are a great way to keep track of dates and times in PHP. The timestamp is the amount of time in seconds from January 1st 1970. This makes each timestamp a unique number in seconds from this fixed date.

Getting the Timestamp
First off, we need to get a timestamp and to get a timestamp we need to use the PHP time() function. This will give us the exact time in seconds this instant, which is 1436780717 seconds from 1st January 1970. This does not look too great and the date can not be worked out from this by the human eye, so we need to convert this into a readable format and luckily for us, PHP can do this aswell!

Get the Timestamp code: <?php $php_timestamp time(); ?>

Converting the Timestamp to a readable date format

Converting the timestamp we have already created, is very straightforward witrh PHP, we just need to work out how we want it displayed. This is done using the PHP Date() function, as we have the number in seconds, we can change this to any dd/mm/yyyy or mm/dd/yyyy layout so I will go through a few of them for you.


dd/mm/yyyy 15/12/2017 <?php $php_timestamp_date date("d/m/Y"$php_timestamp);
  echo 
"".$php_timestamp_date.""?>
mm/dd/yyyy 12/15/2017 <?php $php_timestamp_date date("m/d/Y"$php_timestamp);
  echo 
"".$php_timestamp_date.""?>
Without mins/seconds 15 December 2017 <?php $php_timestamp_date date("d F Y"$php_timestamp);
  echo 
"".$php_timestamp_date.""?>
With mins/seconds 15 December 2017 04:10:54 <?php $php_timestamp_date date("d F Y H:i:s"$php_timestamp);
  echo 
"".$php_timestamp_date.""?>

A full list of various date display options can be found at PHP.net Date Information


So adding it all together we get the following:


The timestamp is 1513311054
The timestamp in a readable format is 15 December 2017

 <?php
$php_timestamp 
time();
$php_timestamp_date date("d F Y"$php_timestamp);
echo 
"The timestamp is ".$php_timestamp."<br />";
echo 
"The timestamp in a readable format is ".$php_timestamp_date."";
 
?>



Free Quote available for any custom PHP and Database Software, Contact Steve Dawson Today