Reputation: 10162
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
?
Upvotes: 242
Views: 635391
Reputation: 101
The DateTime class takes a string in the constructor. If you prefix the timestamp with a @-character you create a DateTime object with the timestamp. For formating use the 'c' format ... a predefined ISO 8601 compound format.
If could use the DateTime class like this ... set the right timezone or leave it out if you want a UTC time.
$dt = new DateTime('@1333699439');
$dt->setTimezone(new DateTimeZone('America/New_York'));
echo $dt->format('c');
Upvotes: 9
Reputation: 6524
most people doesn't read comment, and there is problem with gmdate()
in accepted answer, it return time in GMT, so if we use date_default_timezone_set(zone)
it will not work, instead use date()
.
<?php
$ts = 1664706166;
$date = date('Y-m-d H:i:s', $ts);
$gmdate = gmdate('Y-m-d H:i:s', $ts);
echo "date() : $date\n";
echo "gmdate(): $gmdate\n\n";
date_default_timezone_set("Asia/Jakarta");
$date = date('Y-m-d H:i:s', $ts);
$gmdate = gmdate('Y-m-d H:i:s', $ts);
echo "date() : $date\n";
echo "gmdate(): $gmdate <- GMT\n";
/* result
date() : 2022-10-02 10:22:46
gmdate(): 2022-10-02 10:22:46
After set timezone
date() : 2022-10-02 17:22:46
gmdate(): 2022-10-02 10:22:46 <- GMT
*/
Upvotes: 5
Reputation: 81
You can do like as.....
$originalDate = "1585876500";
echo $newDate = date("Y-m-d h:i:sa", date($originalDate));
Upvotes: 6
Reputation: 42612
Try gmdate
like this:
<?php
$timestamp=1333699439;
echo gmdate("Y-m-d\TH:i:s\Z", $timestamp);
?>
Upvotes: 402
Reputation: 1856
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.
Upvotes: 1
Reputation: 862
<?php
$timestamp=1486830234542;
echo date('Y-m-d H:i:s', $timestamp/1000);
?>
Upvotes: 27
Reputation: 603
It is very important to set a default timezone to get the correct result
<?php
// set default timezone
date_default_timezone_set('Europe/Berlin');
// timestamp
$timestamp = 1307595105;
// output
echo date('d M Y H:i:s Z',$timestamp);
echo date('c',$timestamp);
?>
Online conversion help: http://freeonlinetools24.com/timestamp
Upvotes: 28
Reputation: 96
$unixtime_to_date = date('jS F Y h:i:s A (T)', $unixtime);
This should work to.
Upvotes: 7
Reputation: 6096
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
Upvotes: 75
Reputation: 6092
use date function date ( string $format [, int $timestamp = time() ] )
Use date('c',time())
as format to convert to ISO 8601 date (added in PHP 5) - 2012-04-06T12:45:47+05:30
use date("Y-m-d\TH:i:s\Z",1333699439)
to get 2012-04-06T13:33:59Z
Here are some of the formats date function supports
<?php
$today = date("F j, Y, g:i a"); // March 10, 2001, 5:16 pm
$today = date("m.d.y"); // 03.10.01
$today = date("j, n, Y"); // 10, 3, 2001
$today = date("Ymd"); // 20010310
$today = date('h-i-s, j-m-y, it is w Day'); // 05-16-18, 10-03-01, 1631 1618 6 Satpm01
$today = date('\i\t \i\s \t\h\e jS \d\a\y.'); // it is the 10th day.
$today = date("D M j G:i:s T Y"); // Sat Mar 10 17:16:18 MST 2001
$today = date('H:m:s \m \i\s\ \m\o\n\t\h'); // 17:03:18 m is month
$today = date("H:i:s"); // 17:16:18
?>
Upvotes: 148