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…

Anil Kumar Panigrahi

With more than 10 years experience in PHP and Founder of Anil Labs, a blog for PHP and related posts. contributed posts regarding CodeIgniter, CakePHP and Learn PHP online

Contact : [email protected]

You may also like...

7 Responses

  1. Pradip says:

    Thanks
    Nice statistics calculation using php .But i need how to use these and create graph. Please send me reply as soon as possible.

  2. Bing says:

    Hi Anil,

    Thanks for your script, but would you let me know where to get it’s database?
    many thanks,
    Cheers,
    Bing

  3. tyra says:

    how u do this statistic? i dont understand… how u make this graph?

  4. Nuruzzaman says:

    Hi Anil,

    well,i want this database form

  5. parthan says:

    YES, we need to get the database according to the data mont and year.

  6. Michel says:

    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.

  1. October 29, 2011

    […] graphs using fusion charts. And implemented using PHP. In my previous post explained about How to calculate statistics in daily, weekly , monthly and yearly using php. Get the information using that post and implement the graph using this code. Create a xml file […]

Leave a Reply

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