2012-11-16 54 views
2

執行查詢後Mongodate轉換

$orders = Orders::connection()->connection->command(array(
    'aggregate' => 'orders', 
    'pipeline' => array( 
    array( 
     '$group' => array( 
     '_id' => array('cust_id'=>'$cust_id','ord_date'=>'$ord_date'), 
     'total' => array('$sum' => '$price') , 
     ), 
    ), 
    ) 
));  

我得到的$輸出數組訂單爲

Array 
(
    [0] => Array 
     (
      [_id] => Array 
       (
        [cust_id] => xyz123 
        [ord_date] => MongoDate Object 
         (
          [sec] => 1351875851 
          [usec] => 102000 
         ) 

       ) 

      [total] => 75 
     ) 

    [1] => Array 
     (
      [_id] => Array 
       (
        [cust_id] => abc123 
        [ord_date] => MongoDate Object 
         (
          [sec] => 1351875851 
          [usec] => 102000 
         ) 

       ) 

      [total] => 125 
     ) 

) 

我試圖與MongoDate對象以下正確打印

foreach($orders['result'] as $o){ 
    $date = $o['ord_date']; 
    echo date('Y-M-d',$date->sec); 
} 

也試過

foreach($orders['result'] as $o){ 
    $date = new MongoDate ($o['ord_date']); 
    echo date('Y-M-d',$date->sec); 
} 

有人能告訴我正確的方法嗎?

回答

3

您錯過了對象的_id級別。試試這個:

foreach($orders['result'] as $o){ 
    $date = $o['_id']['ord_date']; 
    echo date('Y-M-d',$date->sec); 
} 
+0

謝謝......我真的錯過了_id –