Category: PHP5

Added: 18th of December 2015

Viewed: 2033 times

Convert a Unix timestamp to date and time using PHP

To convert a unix timestamp to date and time using php we use the php date() and time() functions

The time() function returns the current time as a Unix timestamp (the number of seconds elapsed since January 1 1970 00:00:00 GMT).

In our code below we are displaying the date and time in the following format
(d=day of the month)(F=month)(Y=year, 4 digits) - (G=hour)(i=minutes)(s=seconds)(a=am or pm)

<?php
echo $date = date("d F Y - G:i:s a",time());
?>

If you wanted to grab the time in seconds from your database you would use the following code
<?php
echo $date = date("d F Y - G:i:s a",$row['field_name']);
?>

Other Tips & Tutorials

PHP5

Convert all characters to Upper or Lower case in PHP5

Posted: 7th of October 2020

Views: 144

PHP5

Get the day of the week for a specified date using PHP5

Posted: 17th of January 2016

Views: 1774

PHP5

How to upload files larger than 2MB using PHP5

Posted: 14th of January 2016

Views: 1944

PHP5

Preserve line breaks in PHP. nl2br function

Posted: 21st of December 2015

Views: 3504

PHP5

Convert a Unix timestamp to date and time using PHP

Posted: 18th of December 2015

Views: 2033

PHP5

How to validate a form using PHP5

Posted: 15th of December 2015

Views: 1055

PHP5

Count the number of elements in a PHP array using the count() function

Posted: 15th of December 2015

Views: 892

PHP5

Pass ampersand through query strings in URL using PHP5

Posted: 27th of November 2015

Views: 2156