我有这个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); ?>
我应该用什么?
此时间戳以毫秒为单位,而不是以秒为单位。除以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
你可以用DateTime::createFromFormat实现这一目标。
因为你有一个timestamp
与13 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
JavaScript中使用13位数的时间戳来表示以毫秒为单位的时间。在PHP 10中,数字时间戳用于表示以秒为单位的时间。因此除以1000并四舍五入得到10位数。
$timestamp = 1443852054000;
echo date('Y-m-d h:i:s', floor($timestamp / 1000));