I have to make in my model a query to order by this subtraction between two dates.
I have to calculate subtraction between columns ordersheader.transportDate
and orderitems.forecast_hour
. These are in 2 tables - ordersheader
and orderitems
. I've joined them ON idOrder
.
First, I have to find max(orderitems.forecast_hour
) for each idOrder and then to subtract ordersheader.transportDate
- this max value from orderitems.forecast_hour
for each idOrder. It should be calculated in hours because it's the same date.
It should be displayed first, these idOrders which orderitems.transportDate
- this max orderitems.forecast_hour
< 0.
I tried with this but it gave me error:
Call to undefined function DATEDIFF
Ho to do that?
<?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();
}
Try the following changes. DATEDIFF needs to be in your SELECT line:
$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();