迄今为止在php和mongodb中的时间戳 [英] Timestamp to date in php and mongodb
问题描述
我花了3天的时间来解决这个问题,但没有成功. 我正在使用MongoDB PHP库,并尝试使用PHP Docs中的示例将时间戳转换为有效日期,但始终返回1970-01-17.
I spent 3 days trying to solve this with no success. I'm using the MongoDB PHP Library and i'm trying to convert a timestamp in a valid date using the example in the PHP Docs but it's always returning 1970-01-17.
代码是:
$utcdatetime = new MongoDB\BSON\UTCDateTime(1453939200);
$datetime = $utcdatetime->toDateTime();
var_dump($datetime);
推荐答案
文档 指出,构造函数采用整数参数表示时间戳(以毫秒为单位),您提供的时间戳以秒为单位,因此无效的日期结果.
The documentation states that the constructor takes in an integer parameter representing the timestamp in milliseconds, you are providing a timestamp in seconds hence the invalid date result.
将值乘以1000得到时间戳(以毫秒为单位),因此返回转换后的有效datetime对象:
Multiply the value by 1000 to get the timestamp in milliseconds thus return a valid datetime object converted:
$timestamp = 1453939200 * 1000;
$utcdatetime = new MongoDB\BSON\UTCDateTime($timestamp);
$datetime = $utcdatetime->toDateTime();
var_dump($datetime);
这篇关于迄今为止在php和mongodb中的时间戳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!