i've a filter which gets the data from my DB on date filter base which finds the data between two dates. e.g from = 2013-12-15 , to = 2013-12-20 etc. and return me resutl with date on which my required data is found.
now i want if user selects the dates which are greater than 20 days like 25 days selected from date filter or more than 1 month which could be lead upto 5 months then it should be converted into WEEKS format and it give me back data on week base and show me data against week number.
<?php $date_from = 2012-12-01; $to_date = 2013-12-31; ?>
like in above i have one month or 4 week . now i want to show data on week base as i have more than 20 day difference between above two dates.
i am applying this in Google area chart. now i want to show data in weeks base rather on single date base.
i need solution in php, mysql.
my code
this is my controller function which gets the post date values
public function get_sales_graph(){
if($this->input->post()){
$type = $this->input->post('type');
$get_date = str_replace("/", "-", $this->input->post('chosen_from_date'));
$get_date = DateTime::createFromFormat('m-d-Y', $get_date);
$from_date = $get_date->format('Y-m-d');
$get_to_date = str_replace("/", "-", $this->input->post('chosen_to_date'));
$get_to_date = DateTime::createFromFormat('m-d-Y', $get_to_date);
$to_date = $get_to_date->format('Y-m-d');
$date1 = new DateTime($from_date);
$date2 = new DateTime($to_date);
$interval = $date1->diff($date2);
$day_count = $interval->format('%a');
}
$user_id = $this->user->id;
$rows['graph'] = $this->events_model->my_promo_graph_model($from_date, $to_date, $user_id);
$result = count($rows['graph']);
$data['data'] = array();
if($result > 0){
array_push($data['data'], array('DATE', 'Sales'));
foreach($rows['graph'] as $row){
array_push($data['data'], array($row['date_display'], (int)$row['ticketss_sold_on_date']));
}
}
else{
$i = 1;
$day_count = $day_count == 0 ? 1 : $day_count;
array_push($data['data'], array('DATE', 'Sales'));
while($i <= $day_count){
array_push($data['data'], array(date('Y-m-'.$i), 0 ));
$i++;
}
}
echo json_encode($data);
}
and here is my model
public function my_promo_graph_model($from_date, $to_date, $user_id){
$this->db->select('DATE(tickets_sold.date) AS date_display,
SUM(tickets_sold.quantity) AS ticketss_sold_on_date,
SUM(tickets_sold.quantity*tickets_sold.price) AS money_earned');
$this->db->join('tickets_sold', 'tickets_sold.event_date_id = my_promos.event_id');
$this->db->where('my_promos.user_id', $user_id);
$this->db->where('DATE(tickets_sold.date) >=', $from_date);
$this->db->where('DATE(tickets_sold.date) <=', $to_date);
$this->db->group_by('date_display');
$query = $this->db->get('my_promos');
return $query->result_array();
}