如何在 Codeigniter 中减去两个日期(从数据库中的 2 列)

How to subtract two dates (from 2 columns in database) in Codeigniter

我必须在我的模型中进行查询,以通过两个日期之间的减法来排序。 我必须计算 ordersheader.transportDateorderitems.forecast_hour 列之间的减法。这些在 2 个表中 - ordersheaderorderitems。我在 idOrder.
加入了他们 首先,我必须为每个 idOrder 找到 max(orderitems.forecast_hour),然后从每个 idOrder 的 orderitems.forecast_hour 中减去 ordersheader.transportDate - 这个最大值。它应该以小时为单位计算,因为它是同一天。 它应该首先显示,这些 idOrders which orderitems.transportDate - this max orderitems.forecast_hour < 0。 我试过这个但它给了我错误:

Call to undefined function DATEDIFF

何去何从?

<?php
 function getOrders(){
            $date = new DateTime("now");
            $curr_date = $date->format('Y-m-d');
            $this->db->select('orderitems.eggSize as size');
            $this->db->select('ordersheader.*,customer.name,GROUP_CONCAT(orderitems.itemNumber) as itemNumber');
            $this->db->select('ordersheader.*,customer.name,ordersheader.*,customer.name,GROUP_CONCAT(orderitems.quantity ) as quantity ');
            $this->db->select('ordersheader.*,customer.name,ordersheader.*,customer.name,GROUP_CONCAT(orderitems.unitPrice) as unitPrice');
            $this->db->select('ordersheader.*,customer.name,ordersheader.*,customer.name,GROUP_CONCAT(orderitems.eggSize ) as eggSize');
            $this->db->select('GROUP_CONCAT(orderitems.forecast_hour) as forecast_hour');
            $this->db->from('ordersheader'); 
            $this->db->join('orderitems', 'orderitems.idOrder = ordersheader.idOrder');
            $this->db->join('customer', 'customer.idCustomer = ordersheader.idCustomer');
            $this->db->where('DATE(orderDueDate)', $curr_date);
            $this->db->group_by('orderitems.idOrder');
            $this->db->order_by(DATEDIFF('hour', 'ordersheader.transportDate', 'orderitems.forecast_hour'));
            $query = $this->db->get();
            return $query->result();
        }

尝试以下更改。 DATEDIFF 需要在您的 SELECT 行中:

$date = new DateTime("now");
$curr_date = $date->format('Y-m-d');
$this->db->select('orderitems.eggSize as size');
$this->db->select('DATEDIFF("hour", "ordersheader.transportDate", "orderitems.forecast_hour") AS MyDateDiff'); //ADD THIS
$this->db->select('ordersheader.*,customer.name,GROUP_CONCAT(orderitems.itemNumber) as itemNumber');
$this->db->select('ordersheader.*,customer.name,ordersheader.*,customer.name,GROUP_CONCAT(orderitems.quantity ) as quantity ');
$this->db->select('ordersheader.*,customer.name,ordersheader.*,customer.name,GROUP_CONCAT(orderitems.unitPrice) as unitPrice');
$this->db->select('ordersheader.*,customer.name,ordersheader.*,customer.name,GROUP_CONCAT(orderitems.eggSize ) as eggSize');
$this->db->select('GROUP_CONCAT(orderitems.forecast_hour) as forecast_hour');
$this->db->from('ordersheader');
$this->db->join('orderitems', 'orderitems.idOrder = ordersheader.idOrder');
$this->db->join('customer', 'customer.idCustomer = ordersheader.idCustomer');
$this->db->where('DATE(orderDueDate)', $curr_date);
$this->db->group_by('orderitems.idOrder');
$this->db->order_by(MyDateDiff); //EDIT THIS

$query = $this->db->get();
return $query->result();