Convert PHP Timestamp to Date Format
PHP Code Snippets > Convert PHP Timestamp to Date Format
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.
|Without mins/seconds||18 October 2017||
|With mins/seconds||18 October 2017 02:05:11||
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 1508292311