2017-06-04 132 views
0

我想使用PHP et MongoDB Driver在MongoDB集合中存儲日期。如何從php腳本正確存儲MongoDB isodate

這裏是我的腳本部分:

1聯接:

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"; 
    } 
} 

的$ queryArray屬性包含我想創建鍵/值對。

已添加可管理日期並將其轉換爲MongoDB的轉換方法ISODate: use \ MongoDB \ BSON \ UTCDateTime as MongoUTCDate; ... ... 公共靜態函數toISODate($ date){ if(is_object($ date)){ $ dateToString = $ date-> year。 「 - 」。 $ date-> month。 「 - 」。 $日期 - >天; } else { $ dateToString = substr($ date,0,10); } $ initDate = new \ DateTime($ dateToString);

#begin_debug 
    #echo "Date from string " . $dateToString . " : " . $initDate->format("d-m-Y") . " (timestamp) " . $initDate->getTimestamp() . "<br />\n"; 
    #end_debug 

    return new MongoUTCDate($initDate->getTimestamp()); 
} 

我的控制器的工作原理是這樣的:

  $stats->_id = $this->requestData()->_id; 

     $stats->purchases = [array(
      "date" => \wp\Database\MongoDB\Utilities\MongoDate::toIsoDate($this->requestData()->date), 
      "coords" => array("lat" => $this->requestData()->coords->lat, "lon" => $this->requestData()->coords->lon), 
      "metar" => null, 
      "quantity" => $this->requestData()->quantity, 
      "price" => (float) $this->requestData()->price, 
      "peremption" => \wp\Database\MongoDB\Utilities\MongoDate::toIsoDate($this->requestData()->peremption) 
     )]; 

     $stats->insert(); 

調用時,正確地創建文檔,但日期是錯誤的:

{ 
"_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 /> 

如果轉換回時間戳,得到正確的日期...所以,不明白爲什麼Mongo日期不正確。

回答

0

對於由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; 
} 

正確的日期存儲...