我想找到从订单处理到每个订单标记为完成的订单的时间。我怎么能得到它?我已阅读this,它只给出了上次修改的时间。
答案 0 :(得分:1)
这将获得所有订单,并返回下订单与当前处于已完成状态之间的秒数。您尚未指定时差的格式,因此我将其作为带有相应订单ID的字符串返回。
$query = new WC_Order_Query( array(
'limit' => -1,
'orderby' => 'date',
'order' => 'DESC',
'return' => 'ids',
) );
foreach( $query->get_orders() as $order_id ) {
$order = wc_get_order($order_id);
if( $order->get_status() === 'completed') {
$order_data = $order->get_data();
$timestamp = $order_data['date_modified']->getTimestamp() - $order_data['date_created']->getTimestamp();
$d1 = new DateTime();
$d2 = new DateTime();
$d2->add(new DateInterval('PT'.$timestamp.'S'));
$order_interval = $d2->diff($d1);
echo 'Order ID : ' . $order->get_id() . ' ' . $order_interval->format('%a days, %h hours, %i minutes and %s seconds');
}
}
答案 1 :(得分:0)
$order_id = 1945;
$order = new WC_Order($order_id);
$order_data = $order->get_data();
$date1 = $order_data['date_created']->date( 'Y-m-d G:i:s' );
$date2 = $order_data['date_modified']->date( 'Y-m-d G:i:s' );
$diff = abs(strtotime($date2) - strtotime($date1));
$years = floor($diff / (365*60*60*24));
$months = floor(($diff - $years * 365*60*60*24) / (30*60*60*24));
$days = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));
printf("%d years, %d months, %d days\n", $years, $months, $days);
您可以通过添加对订单状态的检查来改进此功能。