237

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?

Xenon
  • 3,174
  • 18
  • 37
Utku Dalmaz
  • 9,780
  • 28
  • 90
  • 130

10 Answers10

394

Try gmdate like this:

<?php
$timestamp=1333699439;
echo gmdate("Y-m-d\TH:i:s\Z", $timestamp);
?>
Flimm
  • 136,138
  • 45
  • 251
  • 267
stewe
  • 41,820
  • 13
  • 79
  • 75
  • 80
    Please not that `gmdate()` returns the date/time in GMT. `date()` will return the local date/time. – Matt K Dec 10 '15 at 16:37
  • 11
    Above comment doesn't make sense in this context. Using date with the second parameter as timestamp, as shown in answer, will not print the date in local timezone. Date will be printed with timezone info ignored, cause the timestamp doesn't have timezone info. gmdate and date with second timestamp parameter as 1333699439 will print the same date time. – Tinus Tate Apr 07 '16 at 13:09
  • 2
    @MattK: Which makes sense, since Z indicate UTC. Tinus: Unix timestamps do not contain timezone offsets. `date` and `gmdate`'s output will differ based on the TZ variable (on *nix at least) – Gert van den Berg May 03 '16 at 14:05
  • You can check this http://www.phpzag.com/convert-unix-timestamp-to-readable-date-time-in-php/ – Laeeq May 04 '17 at 08:08
  • It would also be nice if this answer included the solution for the opposite problem, converting a formatted date to UNIX time. – Olle Härstedt Sep 14 '18 at 14:20
  • `$date = new DateTime('2018-01-01 00:00:00'); $date->format('U');` – Olle Härstedt Sep 14 '18 at 14:30
  • PHP v5.6 generates from timestamp 1540426286647 -> 50784-03-01 but should be 2018-10-25. When using gmdate('Y-m-d', $lastmodifieddate_ts); – Kaspar L. Palgi Oct 25 '18 at 00:34
  • 1
    @KasparL.Palgi You would have to divide the timestamp by 1000 to get proper year, as this timestamp is stored in milliseconds. – Wadih M. Jul 01 '20 at 09:43
142

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
?>
Sandeep Manne
  • 6,030
  • 5
  • 39
  • 55
  • 1
    how can i convert specific timestamp with 'c'? for example 1333699439 – Utku Dalmaz Apr 06 '12 at 07:18
  • this doesnt work date("Y-m-d\TH:i:s\Z",1333699439) i got 2012-04-06EDT04:03:59-14400 – Utku Dalmaz Apr 06 '12 at 07:32
  • @Ahmet - Note use of double and single quotes when defining the string – Mark Baker Apr 06 '12 at 08:44
  • Thank you for the detailed answer. Just a questions: date("Y-m-d\TH:i:s\Z",1333699439) to get 2012-04-06T13:33:59Z --- What is that T and Z? I guess T is for time and Z for? Thank you again. – user2060451 Nov 27 '13 at 04:20
  • 1
    @user2060451 anything preceded by a backslash is shown as the literal character in the resulting time string – pulsar Jan 24 '15 at 21:53
  • 1
    @user2060451: Z is supposed to indicate UTC time (in which case gmdate should be used...) Check out ISO8601 info on your favourite research resource. – Gert van den Berg May 03 '16 at 14:08
73

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

liquorvicar
  • 6,081
  • 1
  • 16
  • 21
  • 2
    how can i convert specific timestamp with this? for example 1333699439 – Utku Dalmaz Apr 06 '12 at 07:17
  • 1
    @Norse Of course I read his post. All the info he needed was in my post. All he had to do was read the PHP docs for the DateTime object. I've edited my post to make it even clearer. The DateTime object is the way to go in PHP and all newbies should be encouraged to use it. As yet, I'm the only one who's provided that answer. Btw I find your comment a bit aggressive and not quite in the spirit of SO. – liquorvicar Apr 06 '12 at 07:34
  • 1
    @Ahmetvardar See my edit although all this is in the PHP docs to which I provided a link. I strongly urge you to start using the DateTime object. It provides excellent support for dates/times including timezones etc – liquorvicar Apr 06 '12 at 07:35
27

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

klit67
  • 595
  • 6
  • 5
25
<?php
$timestamp=1486830234542;
echo date('Y-m-d H:i:s', $timestamp/1000);
?>
sunil
  • 832
  • 15
  • 25
  • 6
    Works without the divide part. Otherwise the date defaults to 1970. – Script47 Apr 29 '18 at 16:11
  • This works on me. Idk why others says it doesn't work. Prob because I'm using PHP7.2? – Lapiz the Programmer Mar 01 '21 at 10:45
  • @LapiztheProgrammer yeah its depend on PHP versions – sunil Mar 02 '21 at 11:19
  • 2
    I'm getting the value from the external API where it looks like: '1623923723000'. In this case, the dividing part is necessary to get the correct date: '2021-06-17 11:55:23'. Thanks for sharing the solution. – WojCup Jun 17 '21 at 11:22
  • The value must be in milliseconds. date must be assuming a unix timestamp in seconds. – Tom Jul 03 '22 at 03:20
  • for me the result needs to add `date_default_timezone_set("Asia/Tehran");` to work correct – Ali Aug 09 '22 at 13:26
9

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');
Team EJ
  • 101
  • 1
  • 2
7
$unixtime_to_date = date('jS F Y h:i:s A (T)', $unixtime);

This should work to.

6

You can do like as.....

$originalDate = "1585876500";

echo $newDate = date("Y-m-d h:i:sa", date($originalDate));
THess
  • 1,003
  • 1
  • 13
  • 21
4

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
*/
uingtea
  • 6,002
  • 2
  • 26
  • 40
1

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.

Samuel Ramzan
  • 1,770
  • 1
  • 15
  • 24