Convert timestamp to date php

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.

Convert timestamp to date php

asked Mar 6, 2011 at 21:28

0

Use PHP's date() function.

Example:

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

answered Mar 6, 2011 at 21:31

gen_Ericgen_Eric

217k40 gold badges295 silver badges334 bronze badges

1

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().

answered Mar 6, 2011 at 21:30

Explosion PillsExplosion Pills

185k49 gold badges317 silver badges394 bronze badges

2

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

answered Dec 17, 2014 at 22:54

Razan PaulRazan Paul

13.2k3 gold badges66 silver badges59 bronze badges

$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:

  • Africa
  • America
  • Antarctica
  • Arctic
  • Asia
  • Atlantic
  • Australia
  • Europe
  • Indian
  • Pacific
  • Others

answered Jun 11, 2016 at 13:40

Convert timestamp to date php

0

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

Convert timestamp to date php

Top-Master

5,8145 gold badges29 silver badges53 bronze badges

answered Sep 28, 2013 at 10:01

2

Try this one:

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

Francesco Boi

7,46412 gold badges68 silver badges109 bronze badges

answered Mar 6, 2019 at 10:04

2

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.

answered Oct 14, 2017 at 18:28

PascalPascal

5066 silver badges13 bronze badges

2

$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

answered Feb 11, 2019 at 6:37

Convert timestamp to date php

Try it.


answered Feb 8, 2015 at 4:33

Convert timestamp to date php

Neil DhakalNeil Dhakal

3634 silver badges11 bronze badges

You can try this:

   $mytimestamp = 1465298940;

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

Output :

06-07-2016

Convert timestamp to date php

チーズパン

2,7488 gold badges40 silver badges61 bronze badges

answered Jun 7, 2016 at 11:37

Convert timestamp to date php

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);

answered Oct 12, 2016 at 15:39

Convert timestamp to date php

bishopbishop

35.5k10 gold badges100 silver badges133 bronze badges

1

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

M.M

2,1041 gold badge19 silver badges31 bronze badges

answered Jul 7, 2016 at 11:05

Convert timestamp to date php

1

echo 'Le '.date('d/m/Y', 1234567890).' à '.date('H:i:s', 1234567890);

Convert timestamp to date php

Bhargav Rao

47.3k27 gold badges121 silver badges137 bronze badges

answered Oct 29, 2016 at 14:23

Red OuaneRed Ouane

511 silver badge1 bronze badge

How do I convert a timestamp to a date?

You can simply use the fromtimestamp function from the DateTime module to get a date from a UNIX timestamp. This function takes the timestamp as input and returns the corresponding DateTime object to timestamp.

What does date () do in PHP?

The date/time functions allow you to get the date and time from the server where your PHP script runs. You can then use the date/time functions to format the date and time in several ways.

How can get date in dd

php //Set the default timezone to UTC. date_default_timezone_set('UTC'); echo "Display current date dd/mm/yyyy format ". "
"; echo date("d/m/Y")
.

What does Strtotime mean in PHP?

Definition and Usage The strtotime() function parses an English textual datetime into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 GMT). Note: If the year is specified in a two-digit format, values between 0-69 are mapped to 2000-2069 and values between 70-100 are mapped to 1970-2000.