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


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

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

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

不适合我,还有这个

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

和this:

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

我应该用什么?

时间戳的单位是毫秒,而不是秒。除以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

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

$timestamp = 1443852054000;
echo date('Y-m-d h:i:s', floor($timestamp / 1000));

您可以使用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

http://sandbox.onlinephpfunctions.com/code/d0d01718e0fc02574b401e798aaa201137658acb


您可能需要设置默认时区以避免任何警告

date_default_timezone_set('Europe/Lisbon');

注意:

关于php日期和时间在php正确的方式