Articles Database General Category mysql PHP PHP Code

How to calculate statistics in daily, weekly , monthly and yearly using php

In this post i am going to explain about how to calculate statistics in daily, weekly , monthly and yearly using php. Recently i have worked in this concept. In the administration section we need to display these type statistics. I will explain graphs in the next post. Get these information from the database and using php to calculate the statistics.

How to calculate statistics in daily, weekly , monthly and yearly using PHP by Anil Kumar Panigrahi
How to calculate statistics in daily, weekly , monthly and yearly using PHP by Anil Kumar Panigrahi

If we choose days, then it is calculate the difference between from date and to date using previous post How to calculate the list of dates between two dates . After calculate the days we can easily get the list of records from database and draw the your graphs.

If we choose weeks, then it calculate the week days from today. In this section we need to get the today’s week name and calculate the remaining. For these calculations i have used mysql.

 

PHP Code

<?php
    $date_we = date('Y-m-d');
    $get_week_days = "select   DAYNAME(date_sub('$date_we', interval 6 day)) as DAY_START,
 date_sub('$date_we', interval 6 day) As sel_date_start,
                               DAYNAME(date_sub('$date_we', interval 5 day)) as DAY_2,
     date_sub('$date_we', interval 5 day) As sel_date_start2,
                               DAYNAME(date_sub('$date_we', interval 4 day)) as DAY_3,
    date_sub('$date_we', interval 4 day) As sel_date_start3,
                               DAYNAME(date_sub('$date_we', interval 3 day)) as DAY_4,
     date_sub('$date_we', interval 3 day) As sel_date_start4,
                               DAYNAME(date_sub('$date_we', interval 2 day)) as DAY_5,
     date_sub('$date_we', interval 2 day) As sel_date_start5,
                               DAYNAME(date_sub('$date_we', interval 1 day)) as DAY_6,
    date_sub('$date_we', interval 1 day) As sel_date_start6,
 DAYNAME('$date_we') as DAY_END,
     date_sub('$date_we', interval 0 day) As sel_date_end"
;
    $result_week_days = mysql_query($get_week_days);                           
    while($res_get_week_days = mysql_fetch_assoc($result_week_days)){
        $dataFeildsArray[] = $res_get_week_days['DAY_START']." - " . $res_get_week_days['sel_date_start'];
        $dataFeildsArray[] = $res_get_week_days['DAY_2']." - " . $res_get_week_days['sel_date_start2'];
        $dataFeildsArray[] = $res_get_week_days['DAY_3']." - " . $res_get_week_days['sel_date_start3'];
        $dataFeildsArray[] = $res_get_week_days['DAY_4']." - " . $res_get_week_days['sel_date_start4'];
        $dataFeildsArray[] = $res_get_week_days['DAY_5']." - " . $res_get_week_days['sel_date_start5'];
        $dataFeildsArray[] = $res_get_week_days['DAY_6']." - " . $res_get_week_days['sel_date_start6'];
        $dataFeildsArray[] = $res_get_week_days['DAY_END']." - " . $res_get_week_days['sel_date_end'];
       
    }
   
    print_r($dataFeildsArray);
   
   
?>

If we choose month, first calculate the how many days for that particular month. To calculate the days in a month using following function
 

PHP Code

 $month = date('m');
 $year = date('Y');
 for($m=1;$m<=cal_days_in_month(CAL_GREGORIAN, $month, $year);$m++)
   {
    if($m <10){ $mon="0".$m; }
    else { $mon = $m;}
    $mon_days[] = $year.'-'.$month.'-'.$mon;
   }
       
  print_r($mon_days);

We will get the list of days in a particular month and calculate the list of dates in that month.

If we choose year, then it is calculate the month wise or week wise.
 

1) Calculate month wise dates from current month

       $total_year = 11;
    $current_d = date('m.d');
    for($mon=$total_year;$mon>=0;$mon--){
       $sel_query = "select DATE_FORMAT(DATE_SUB( '".$year.".".$current_d."' , INTERVAL $mon MONTH ),'%Y.%M') as months";
    $rmon = mysql_query($sel_query);
        while($res_months=mysql_fetch_assoc($rmon)){
           $months[] = $res_months['months'];
           $disp_month = explode('.',$res_months['months']);
           $disp_months[] = $disp_month[1];
           
        }
    }
   
    print_r($disp_months);

 

2) By week wise details. weekly report code by following

 for($i=52;$i>=0;$i--){
          $disp_times[$i] =  date('Y-m-d', strtotime('-'.$i.' week',strtotime($year.'-12-31') ));
    }    
     
     print_r($disp_times);

We can write mysql quires using with these dates and get the details. and in the next post I will explain about how to implement graphs using fusion charts and php

Hope it will be useful for statistics pages for admin section…

7 thoughts on “How to calculate statistics in daily, weekly , monthly and yearly using php”

  1. No matter if some one searches for his required
    thing, therefore he/she desires to be available that in detail,
    thus that thing is maintained over here.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.