这是我的表,我需要从我的MySQL数据库中获取所有数据,这些数据将显示30天计算每天的订单总数。而且还会有一个日期范围。
示例1:当前日期是2016年8月8日。我需要显示2016年8月8日以来的最后30天。
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
现在预期输出将显示:
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 but still need to include in the output
)
[4] => array (
[date] => 2016-08-04
[total_orders] => 3
)
//and so forth (July 09, 2016).....
)
是否有任何PHP codeigniter代码?
答案 0 :(得分:0)
$this->db->select("DATE_FORMAT(date,'%Y-%m-%d') as covered_date,count(date(date)) AS 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);