我花了3天时间尝试解决这个问题,但没有成功。我正在使用MongoDB PHP库,并且尝试使用PHP文档中的示例将时间戳转换为有效日期,但始终返回1970-01-17。
代码如下:
$utcdatetime = new MongoDB\BSON\UTCDateTime(1453939200);
$datetime = $utcdatetime->toDateTime();
var_dump($datetime);
文档中指出,构造函数需要传入一个表示以毫秒为单位的时间戳的整数参数,而您提供的是以秒为单位的时间戳,因此导致了无效的日期结果。
将值乘以1000以获取以毫秒为单位的时间戳,然后返回转换后的有效日期时间对象:
$timestamp = 1453939200 * 1000;
$utcdatetime = new MongoDB\BSON\UTCDateTime($timestamp);
$datetime = $utcdatetime->toDateTime();
var_dump($datetime);
$utcdatetime = new MongoDB\BSON\UTCDateTime(strtotime($date));
$date2 = new MongoDB\BSON\Timestamp(1, date($utcdatetime));
$ foo = new DateTime(); $ foo-> setTimestamp(1453939200); var_dump($ foo);
- Sammitch