How to convert a 13 digit Unix Timestamp to Date and time?

怎甘沉沦 提交于 2019-11-29 09:21:28
u_mulder

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

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

santhosh

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));
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!