2016-11-11 119 views
1

轉換ISO日期蒙戈我有我的蒙戈文檔中的下列數據:問題使用PHP

"last_assigned" : ISODate("2016-11-10T20:34:36.000Z") 

當我查詢,其中包括日期,如上述文件的數據庫,我試圖將其轉換後,這是我的var_dump顯示:

object(DateTime)[22] 
    public 'date' => string '1970-01-01 00:00:00.000000' (length=26) 
    public 'timezone_type' => int 1 
    public 'timezone' => string '+00:00' (length=6) 

這是我的代碼看起來像 - 「[ 'last_assigned'] $值」 是我從DB回來...

  if (!empty($value['last_assigned'])) { 
       $tempdate = new MongoDate(strtoTime($value['last_assigned'])); 
       var_dump($tempdate->toDateTime());     
      } 

我如何編寫我的代碼,以便它能夠顯示日期的適當值?

編輯1

我試圖改變這樣的代碼:

   $tempdate = (string)$value['last_assigned']->sec; 
       $tempdate = new MongoDate(strtotime($tempdate)); 
       var_dump($tempdate->toDateTime()); 

這就是vardump顯示:

object(DateTime)[22] 
    public 'date' => string '1970-01-01 00:00:00.000000' (length=26) 
    public 'timezone_type' => int 1 
    public 'timezone' => string '+00:00' (length=6) 
+0

嘗試將last_assigned轉換爲字符串,然後再通過strtoTime函數對其進行設置。 – Veeram

+0

你使用的是mongo php驅動程序嗎?或者你只是直接查詢數據?如果直接查詢數據,則可能需要將日期保存爲字符串「2016-11-10T20:34:36.000Z」。所以實質上,新的MongoDate(strtoTime(「2016-11-10T20:34:36.000Z」)應該是這樣的。 – Veeram

+0

@Veeram我使用的是mongoclient驅動程序,我知道它已被棄用...我們將盡快升級一切,但現在......這就是我正在使用的 – Happydevdays

回答