如何从php脚本正确存储MongoDB isodate

如何从php脚本正确存储MongoDB isodate,php,mongodb,date,Php,Mongodb,Date,我想使用PHP et MongoDB驱动程序在MongoDB集合中存储日期 以下是我的部分脚本: 第一连接: public function process(){ try{ $this->connexion = new \MongoDB\Driver\Manager($this->dsn); } catch(\MongoDB\Driver\Exception\InvalidArgumentException $e){ d

我想使用PHP et MongoDB驱动程序在MongoDB集合中存储日期

以下是我的部分脚本:

第一连接:

    public function process(){
    try{
        $this->connexion = new \MongoDB\Driver\Manager($this->dsn);
    }
    catch(\MongoDB\Driver\Exception\InvalidArgumentException $e){
        die("Unable to connect to the MongoDB database : " . $e->getMessage());
    } catch(\MongoDB\Driver\Exception\RuntimeException $e){
        die("General failure : " . $e->getMessage());
    }
}
第二:文档创建

    public function process(){
    $dbConnector = dbConnector::instance(); // Instance de connexion à la base de données


    $query = new \MongoDB\Driver\BulkWrite;

    $query->insert($this->queryArray);

    $writeConcern = new \MongoDB\Driver\WriteConcern(\MongoDB\Driver\WriteConcern::MAJORITY, 1000);
    //$collection = new \MongoCollection($dbConnector->connexion()->db(), $this->store->collectionName());
    try{
        $this->result = $dbConnector->connexion()->executeBulkWrite($dbConnector->dbName() . "." . $this->store->collectionName(), $query, $writeConcern);
    } catch (\MongoDB\Driver\AuthenticationException $e){
        echo "Authentication error : " . $e->getMessage() . "<br />\n";
    } catch(\MongoDB\Driver\ConnextionException $e){
        echo "Connexion error : " . $e->getMessage() . "<br />\n";
    }
    catch(\MongoDB\Driver\Exception\RuntimeException $e){
        echo "Runtime error : " . $e->getMessage() . "<br />\n";
    }
}
调用时,文档创建正确,但日期错误:

{
"_id" : "3256222010007",
"purchases" : [ 
    {
        "date" : ISODate("1970-01-18T07:43:01.706Z"),
        "coords" : {
            "lat" : 43.7294742,
            "lon" : 1.416332
        },
        "metar" : null,
        "quantity" : 1,
        "price" : 2.87,
        "peremption" : ISODate("1970-01-18T22:20:34.800Z")
    }
]
}
在前面的示例中,日期将是,即当天的日期

当我记录数据时,我看到日期在PHP中的格式是正确的:

Date from string 2017-06-04 : 04-06-2017 (timestamp) 1496527200<br />
字符串日期2017-06-04:04-06-2017(时间戳)1496527200

如果转换回时间戳,则获得正确的日期。。。所以,我不明白为什么Mongo日期不正确。

对于相同问题的那些实验,只需将PHP给出的时间戳乘以1000即可:

    public static function toISODate($date){
    if(is_object($date)){
        $dateToString = $date->year . "-" . $date->month . "-" . $date->day;
    } else {
        $dateToString = substr($date,0,10);
    }
    $initDate = new \DateTime($dateToString);

    #begin_debug
    #echo "Date récupérée à partir de " . $dateToString . " : " . $initDate->format("d-m-Y") . " (timestamp) " . $initDate->getTimestamp() . "<br />\n";
    #end_debug
    $stampedDate = $initDate->getTimestamp() * 1000;

    $mongoUTCDate = new MongoUTCDate($stampedDate);

    return $mongoUTCDate;
}
公共静态函数toISODate($date){
如果(是对象($date)){
$dateToString=$date->year.“-”$date->month.“-”$date->day;
}否则{
$dateToString=substr($date,0,10);
}
$initDate=new\DateTime($dateToString);
#开始调试
#echo“Date récupéréeápartir de”。$dateToString.:“$initDate->format(“d-m-Y”)。“(时间戳)”。$initDate->getTimestamp()。“
\n”; #结束调试 $stampedDate=$initDate->getTimestamp()*1000; $mongoUTCDate=新的mongoUTCDate($stampedDate); 返回$mongoUTCDate; }

存储了正确的日期…

感谢提示
乘以1000加上时间戳
Date from string 2017-06-04 : 04-06-2017 (timestamp) 1496527200<br />
    public static function toISODate($date){
    if(is_object($date)){
        $dateToString = $date->year . "-" . $date->month . "-" . $date->day;
    } else {
        $dateToString = substr($date,0,10);
    }
    $initDate = new \DateTime($dateToString);

    #begin_debug
    #echo "Date récupérée à partir de " . $dateToString . " : " . $initDate->format("d-m-Y") . " (timestamp) " . $initDate->getTimestamp() . "<br />\n";
    #end_debug
    $stampedDate = $initDate->getTimestamp() * 1000;

    $mongoUTCDate = new MongoUTCDate($stampedDate);

    return $mongoUTCDate;
}