How to convert a unix timestamp using the PHP date function

If you have a unix timestamp and wish to convert it into a meaningful date and time using PHP then the function you need to use is the date() function. So, let’s assume that the timestamp that you wish to convert is 1075995075. 

We could convert this into a meaningful date/time as follows:

<?php
$timestamp=1075995075;
$thedate= date("d/m/y", $timestamp);
print  $thedate;
?>

When we run the above script the result is that “05/02/04” is printed out on the screen. There may however, be situations in which you would want your timestamp to be displayed in a different time/time format. For example, you may have prefered to have your date coming out as 5th February 2004 or Tuesday 5th Feb. In these cases the thing to do is take advantage of PHP’s ability to translate certain characters into meaningful date/time variables.

The online PHP manual gives you a complete list of all the characters that can be accepted when using the date() function. This can be found at http://uk.php.net/date and the page is a definite Favourites List candidate for those who are intending on referencing the list frequently.

There are an inexhaustible amount of different formats you can use to display the date and time. My personal favourites, when using the date() function include the following:

  • $thedate= date("l jS of F Y at g.i a", $timestamp);
    //gives you the result “Thursday 5th of February 2004 at 3.31 pm”
  • $thedate = date("D M j G:i:s T Y", $timestamp);
    //gives you the result “Thu Feb 5 15:31:15 GMT Standard Time 2004”
  • $thedate= date("jS M Y", $timestamp);
    //gives you the result “5th Feb 2004”.
  • $thedate= date("l jS F Y", $timestamp);
    //gives you the result “Thursday 5th February 2004”
  • $thedate= date("G:i.s", $timestamp);
    //gives you the result “15:04.01” (ie., 24 hour clock with seconds also displayed).
  • $thedate= date("g:i a. T, F j, Y", $timestamp);
    //gives you the result “3:31 pm. GMT Standard Time, February 5, 2004” (i.e., the same format that’s used on the CNN homepage)
  • $thedate= date("l, jS F Y, G:i e", $timestamp);
    //give you the result “Thursday, 5th February 2004, 15:31 GMT” (i.e., the same format that’s used on the BBC news webpage. NOTE: the “e” only works on versions of PHP which are greater than version 5. For those using older versions of PHP, I would recommend using the “T” character instead.

If you want to display the current date and time using the date function, then you can achieve this quite easily by removing the “, $timestamp” section from our date() function. For example:

$thedate= date("l, jS F Y, G:i T");

…would display the current date and time in a format that's similar to the one that's used on the BBC news website.

That's pretty much all there is to it.  Good luck!  (David Connelly)

Live Assistant

Hello there! I'm Rachael and I'm your live helper for today.

How can I help you?

Quote Request

If you are looking for a quick quotation then why not submit a quote request? It only takes a few moments and we always respond quickly.