gpt4 book ai didi

php - Mongodate转换

转载 作者:可可西里 更新时间:2023-11-01 10:01:19 24 4
gpt4 key购买 nike

执行查询后

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

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

最佳答案

您缺少对象的 _id 级别。试试这个:

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

关于php - Mongodate转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13418309/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com