如何将13位Unix时间戳转换为日期和时间?

问题描述 投票:11回答:3

我有这个13位数的时间戳1443852054000,我想转换为日期和时间但不成功。我试过这个代码:

echo date('Y-m-d h:i:s',$item->timestamp); 

不适合我,也不适用

$unix_time = date('Ymdhis', strtotime($datetime ));

还有这个 :

$item = strtotime($txn_row['appoint_date']); 
<?php echo date("Y-m-d H:i:s", $time); ?>

我应该用什么?

php timestamp unix-timestamp
3个回答
25
投票

此时间戳以毫秒为单位,而不是以秒为单位。除以1000并使用date函数:

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

1
投票

你可以用DateTime::createFromFormat实现这一目标。

因为你有一个timestamp13 digits,你必须将其除以1000,以便与DateTime一起使用,即:

$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


您可能希望set the default timezone避免任何警告

date_default_timezone_set('Europe/Lisbon');

注意:

关于php日期和时间的更多信息,请访问php the right way


0
投票

JavaScript中使用13位数的时间戳来表示以毫秒为单位的时间。在PHP 10中,数字时间戳用于表示以秒为单位的时间。因此除以1000并四舍五入得到10位数。

$timestamp = 1443852054000;
echo date('Y-m-d h:i:s', floor($timestamp / 1000));
© www.soinside.com 2019 - 2024. All rights reserved.