php 如何将 13 位 Unix 时间戳转换为日期和时间?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32926749/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to convert a 13 digit Unix Timestamp to Date and time?
提问by glendon philipp Baculio
I have this 13 digit timestamp 1443852054000 that i want to convert to date and time but dont succeed. I have tried this codes:
我有这个 13 位时间戳 1443852054000,我想转换为日期和时间,但没有成功。我试过这个代码:
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?
我应该用什么?
回答by u_mulder
This timestamp is in milliseconds, not in seconds. Divide it by 1000 and use date
function:
此时间戳以毫秒为单位,而不是以秒为单位。除以 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
回答by 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.
JavaScript 中使用 13 位时间戳来表示以毫秒为单位的时间。在 PHP 10 中,数字时间戳用于表示以秒为单位的时间。所以除以 1000 并四舍五入得到 10 位数字。
$timestamp = 1443852054000;
echo date('Y-m-d h:i:s', floor($timestamp / 1000));
回答by CONvid19
You can achieve this with DateTime::createFromFormat.
您可以使用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.:
因为你有一个timestamp
with 13 digits
,你必须将它除以1000
,才能使用它 with 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
http://sandbox.onlinephpfunctions.com/code/d0d01718e0fc02574b401e798aaa201137658acb
You may want to set the default timezoneto avoid any warnings
您可能希望设置默认时区以避免任何警告
date_default_timezone_set('Europe/Lisbon');
NOTE:
注意:
More about php
date and time at php the right way