How to convert a 13 digit Unix Timestamp to Date and time?
Solution 1
This timestamp is in milliseconds, not in seconds. Divide it by 1000 and use date
function:
echo date('Y-m-d h:i:s', $item->timestamp / 1000);
// e.g
echo date('Y-m-d h:i:s',1443852054000/1000);
// shows 2015-10-03 02:00:54
Solution 2
A 13 digit timestamp is used in JavaScript to represent time in milliseconds. In PHP 10 a digit timestamp is used to represent time in seconds. So divide by 1000 and round off to get 10 digits.
$timestamp = 1443852054000;
echo date('Y-m-d h:i:s', floor($timestamp / 1000));
Solution 3
You can achieve this with DateTime::createFromFormat.
Because you've a timestamp
with 13 digits
, you'll have to divide it by 1000
, in order to use it with DateTime
, i.e.:
$ts = 1443852054000 / 1000; // we're basically removing the last 3 zeros
$date = DateTime::createFromFormat("U", $ts)->format("Y-m-d h:i:s");
echo $date;
//2015-10-03 06:00:54
DEMO
http://sandbox.onlinephpfunctions.com/code/d0d01718e0fc02574b401e798aaa201137658acb
You may want to set the default timezone to avoid any warnings
date_default_timezone_set('Europe/Lisbon');
NOTE:
More about php
date and time at php the right way
Related videos on Youtube
glendon philipp Baculio
Updated on August 29, 2020Comments
-
glendon philipp Baculio over 3 years
I have this 13 digit timestamp 1443852054000 that i want to convert to date and time but dont succeed. I have tried this codes:
echo date('Y-m-d h:i:s',$item->timestamp);
doesnt work for me and also this
$unix_time = date('Ymdhis', strtotime($datetime ));
and this :
$item = strtotime($txn_row['appoint_date']); <?php echo date("Y-m-d H:i:s", $time); ?>
what should i use?
-
John Conde over 8 yearsit's in milliseconds. just remove the last three zeros and your code will work.
-
-
glendon philipp Baculio over 8 yearsi am still getting this result 1970-01-01 01:00:00
-
u_mulder over 8 years
var_dump($item->timestamp);
-
u_mulder over 8 yearsI can't say why is it null. You have to check it yourself.
-
Pedro Lobito about 6 yearsTell that to Zuckerberg
-
jasie almost 4 yearsI think, the divided timestamp should be rounded (to provide date() with an int).
-
vahid sabet almost 4 yearsgreat answer bro, Thanks.