299

I have a timestamp stored in a session (1299446702).

How can I convert that to a readable date/time in PHP? I have tried srttotime, etc. to no avail.

0

15 Answers 15

475

Use PHP's date() function.

Example:

echo date('m/d/Y', 1299446702);
1
99

strtotime makes a date string into a timestamp. You want to do the opposite, which is date. The typical mysql date format is date('Y-m-d H:i:s'); Check the manual page for what other letters represent.

If you have a timestamp that you want to use (apparently you do), it is the second argument of date().

2
  • 5
    it would be good note that I passed in my current date as follows: echo date('m/d/Y', strtotime($current_date_variable);
    – cwiggo
    Apr 8, 2015 at 11:26
  • is there no command to convert an timestamp to their datetime standardisation becouse i converted an datetime to an unix timestamp but in my database it is still an datetime i wil try this but i still find it weird that there is no specific command for it Jan 15, 2020 at 14:46
88

I just added H:i:s to Rocket's answer to get the time along with the date.

echo date('m/d/Y H:i:s', 1299446702);

Output: 03/06/2011 16:25:02

74
$timestamp = 1465298940;
$datetimeFormat = 'Y-m-d H:i:s';

$date = new \DateTime();
// If you must have use time zones
// $date = new \DateTime('now', new \DateTimeZone('Europe/Helsinki'));
$date->setTimestamp($timestamp);
echo $date->format($datetimeFormat);

result: 2016-06-07 14:29:00

Other time zones:

1
  • Lately, I've been quite partial to $date = (new DateTimeImmutable())->setTimestamp($timestamp); Sep 30, 2022 at 13:18
25

If you are using PHP date(), you can use this code to get the date, time, second, etc.

$time = time();               // you have 1299446702 in time
$year = $time/31556926 % 12;  // to get year
$week = $time / 604800 % 52;  // to get weeks
$hour = $time / 3600 % 24;    // to get hours
$minute = $time / 60 % 60;    // to get minutes
$second = $time % 60;         // to get seconds
2
  • 13
    How about leap year and second...?
    – Petteri H
    Jan 26, 2015 at 9:22
  • $year = date('Y', $time) Oct 29, 2016 at 14:26
23

If anyone wants timestamp conversion directly to a DateTime object, there's a simple one-liner:

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

Following @sromero comment, timezone parameter (the 3rd param in DateTime::createFromFormat()) is ignored when unix timestamp is passed, so the below code is unnecessary.

$date = DateTime::createFromFormat('U', $timestamp, new DateTimeZone('UTC'); // not needed, 3rd parameter is ignored

You may check PHP's manual for DateTime::createFromFormat for more info and options.

2
  • 1
    Jus a comment about your last line of code: according to the manual page you link: "Note: The timezone parameter and the current timezone are ignored when the time parameter either contains a UNIX timestamp (e.g. 946684800) or specifies a timezone (e.g. 2010-01-28T15:00:00+02:00)."
    – sromero
    Nov 1, 2018 at 12:39
  • @sromero You have right. The timezone parameter is ignored in this case.
    – Pascal
    Nov 2, 2018 at 9:22
20

Try this one:

echo date('m/d/Y H:i:s', 1541843467);
2
  • 1
    Hi there! It seems that this answer has already been proposed; see the top answer.
    – hat
    Mar 6, 2019 at 10:25
  • One line, perfect.
    – MasterD
    May 25, 2022 at 17:23
20
$epoch = 1483228800;
$dt = new DateTime("@$epoch");  // convert UNIX timestamp to PHP DateTime
echo $dt->format('Y-m-d H:i:s'); // output = 2017-01-01 00:00:00

In the examples above "r" and "Y-m-d H:i:s" are PHP date formats, other examples:

Format Output

r              -----    Wed, 15 Mar 2017 12:00:00 +0100 (RFC 2822 date)
c              -----    2017-03-15T12:00:00+01:00 (ISO 8601 date)
M/d/Y          -----    Mar/15/2017
d-m-Y          -----    15-03-2017
Y-m-d H:i:s    -----    2017-03-15 12:00:00
17

Try it.

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

You can try this:

   $mytimestamp = 1465298940;

   echo gmdate("m-d-Y", $mytimestamp);

Output :

06-07-2016

9

Unless you need a custom date and time format, it's easier, less error-prone, and more readable to use one of the built-in date time format constants:

echo date(DATE_RFC822, 1368496604);
1
  • 2
    This should be the top answer.
    – alexg
    Jan 31, 2019 at 11:43
5

echo date("l M j, Y",$res1['timep']);
This is really good for converting a unix timestamp to a readable date along with day. Example: Thursday Jul 7, 2016

1
  • 1
    Similar answers are already posted, what is the plus of information in your answer? Jul 7, 2016 at 11:27
5
echo 'Le '.date('d/m/Y', 1234567890).' &agrave; '.date('H:i:s', 1234567890);
1
I have used this:
<?php echo date('d/m/Y H:i a', $row['start_time']); ?>
0

None of the above answers work properly if the default time zone of your PHP server is not GMT.

For example. Consider this code that round trips a date to a timestamp and back.

 $d0 = new \DateTimeImmutable('1/1/2023');
 echo 'Formatted Jan 1, 2023 date = '.$d0->format('Y-m-d H:i:s').'<br>';
 $t0 = $d0->getTimestamp();
 echo 'Unix timestamp = '.$t0.'<br>';
 $d1 = new \DateTimeImmutable('@'.$t0);
 echo 'Date from timestamp = '.$d1->format('Y-m-d H:i:s').'<br>';

You would expect the round tripped date to be identical to the original date. Wrong! Output is as follows:

Formatted Jan 1, 2023 date = 2023-01-01 00:00:00
Unix timestamp = 1672556400
Date from timestamp = 2023-01-01 07:00:00

This is because the server time zone is America/Arizona. The documentation clearly states that if a DateTime or DateTimeImmutable object is created from a Unix timestamp, the default time zone or a provided time zone is ignored. (Why?! Why?! Why?!)

To fix this, you need to add this...

 $d2 = $d1->setTimeZone(new \DateTimeZone(date_default_timezone_get()));
 echo 'Corrected date from timestamp = '.$d2->format('Y-m-d H:i:s').'<br>';

Which outputs this:

Corrected date from timestamp = 2023-01-01 00:00:00

This nails me about every year. Just now I spent 2 hours trying to figure out why my times are all wrong. I went to my test bench and behold! there is my solution to this problem from a year ago. PHP. pfffft.

Not the answer you're looking for? Browse other questions tagged or ask your own question.