Mongodate转换

时间:2012-11-16 14:12:48

标签: php mongodb lithium

执行查询后

$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') ,
        ),
      ),
    )
));     

我得到$ orders的输出数组

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

有人可以告诉我这样做的正确方法吗?

1 个答案:

答案 0 :(得分:4)

您错过了对象的_id级别。试试这个:

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