在 Mongodb 中以毫秒插入 ISODate
Insert ISODate in Mongodb with milliseconds
我 运行 PHP 7 php_mongodb-1.2.2-7.0-nts-vc14-x64 驱动程序。
$ar = new \MongoDB\BSON\UTCDateTime(strtotime('2017-07-27 06:17:25.123000') * 1000);
上述语句的输出是:
ISODate("2017-07-27T06:17:25.000+0000")
但我也需要毫秒:
ISODate("2017-07-27T06:17:25.123+0000")
由于我是新手,我似乎不知道如何解决这个问题。
strtotime
不支持毫秒
strtotime — Parse about any English textual datetime description into
a Unix timesta
所以 strtotime('2017-07-27 06:17:25.123000')
和 strtotime('2017-07-27 06:17:25')
会 return 相同的结果 1501136245
您必须使用日期时间:
<?php
$date = DateTime::createFromFormat('Y-m-d H:i:s u', '2017-07-27 06:17:25 123000');
$ar = new \MongoDB\BSON\UTCDateTime($date->format('U.u'));
你可以这样做
function mongo_current_datetime(){
$date = new \MongoDB\BSON\UTCDateTime(strtotime('now') * 1000);
return $date;
}
示例输出:
MongoDB\BSON\UTCDateTime Object
(
[milliseconds] => 1610972571000
)
上面的函数以毫秒为单位准备当前日期时间对象,准备插入 mongoDB.
我 运行 PHP 7 php_mongodb-1.2.2-7.0-nts-vc14-x64 驱动程序。
$ar = new \MongoDB\BSON\UTCDateTime(strtotime('2017-07-27 06:17:25.123000') * 1000);
上述语句的输出是:
ISODate("2017-07-27T06:17:25.000+0000")
但我也需要毫秒:
ISODate("2017-07-27T06:17:25.123+0000")
由于我是新手,我似乎不知道如何解决这个问题。
strtotime
不支持毫秒
strtotime — Parse about any English textual datetime description into a Unix timesta
所以 strtotime('2017-07-27 06:17:25.123000')
和 strtotime('2017-07-27 06:17:25')
会 return 相同的结果 1501136245
您必须使用日期时间:
<?php
$date = DateTime::createFromFormat('Y-m-d H:i:s u', '2017-07-27 06:17:25 123000');
$ar = new \MongoDB\BSON\UTCDateTime($date->format('U.u'));
你可以这样做
function mongo_current_datetime(){
$date = new \MongoDB\BSON\UTCDateTime(strtotime('now') * 1000);
return $date;
}
示例输出:
MongoDB\BSON\UTCDateTime Object ( [milliseconds] => 1610972571000 )
上面的函数以毫秒为单位准备当前日期时间对象,准备插入 mongoDB.