php - with - strtotime timezone




Converting a UNIX Timestamp to Formatted Date String (6)

Using PHP, I want to convert UNIX timestamps to date strings similar to this: 2008-07-17T09:24:17Z

How do I convert a timestamp such as 1333699439 to 2008-07-17T09:24:17Z?


Assuming you are using PHP5.3 then the modern way of handling dates is via the native DateTime class. To get the current time you can just call

$currentTime = new DateTime();

To create a DateTime object from a specific timestamp (i.e. not now)

$currentTime = DateTime::createFromFormat( 'U', $timestamp );

To get a formatted string you can then call

$formattedString = $currentTime->format( 'c' );

See the manual page here


I found the information in this conversation so helpful that I just wanted to add how I figured it out by using the timestamp from my MySQL database and a little PHP

 <?= date("Y-m-d\TH:i:s\+01:00",strtotime($column['loggedin'])) ?>

The output was: 2017-03-03T08:22:36+01:00

Thanks very much Stewe you answer was a eureka for me.


Try gmdate like this:

<?php
$timestamp=1333699439;
echo gmdate("Y-m-d\TH:i:s\Z", $timestamp);
?>


$unixtime_to_date = date('jS F Y h:i:s A (T)', $unixtime);

This should work to.


<?php
$timestamp=1486830234542;
echo date('Y-m-d H:i:s', $timestamp/1000);
?>






unix-timestamp