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||23 May 2018||
|With mins/seconds||23 May 2018 00:57:10||
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 1527037030