mysql - How to get all orders last 30 days with date range using codeigniter PHP? -
here table , need fetch data mysql database display 30 days counting total number of orders per day. , there date range.
example 1: current date aug 08, 2016. need display last 30 days aug 08, 2016.
order table id | customer_id | num_of_order |date ---+-------------+--------------+-------- 1 | 10001 | 1 | 2016-08-08 07:23:50 2 | 10002 | 4 | 2016-08-07 11:33:50 3 | 10003 | 2 | 2016-08-06 15:44:50 //same day 4 | 10001 | 5 | 2016-08-06 20:50:50 //same day 5 | 10004 | 3 | 2016-08-04 11:17:50
now expected output display:
array ( [0] => array ( [date] => 2016-08-08 [total_orders] => 1 ) [1] => array ( [date] => 2016-08-07 [total_orders] => 4 ) [2] => array ( [date] => 2016-08-06 [total_orders] => 7 ) [3] => array ( [date] => 2016-08-05 [total_orders] => 0 //no orders still need include in output ) [4] => array ( [date] => 2016-08-04 [total_orders] => 3 ) //and forth (july 09, 2016)..... )
is there php codeigniter code this?
$this->db->select("date_format(date,'%y-%m-%d') covered_date,count(date(date)) total_order"); $this->db->from('order'); $this->db->where('date > date_sub( "2016-08-08" , interval 30 day )'); $this->db->group_by("date(date)"); $query = $this->db->get(); $result = $query->result_array(); $start_date = date('y-m-d', strtotime('-30 day', strtotime($end_date))); $return = array(); while (strtotime($start_date) <= strtotime($end_date)) { $return[] = $start_date; $start_date = date("y-m-d", strtotime("+1 day", strtotime($start_date))); } print_r($return);
Comments
Post a Comment