Calculate daily average for selected period in PHP
The only change I would make is in your two foreach loops. If you see below, I have made the change I would recommend making. This will make the loop a bit shorter and more accurate.

// Store occurrence of same days 
$countDays  = array('Mon' =&gt; 0, 'Tue' =&gt; 0, 'Wed' =&gt; 0, 'Thu' =&gt; 0, 
     'Fri' =&gt; 0, 'Sat' =&gt; 0, 'Sun' =&gt; 0);

// Store values
$output = array('Mon' =&gt; 0, 'Tue' =&gt; 0, 'Wed' =&gt; 0, 'Thu' =&gt; 0, 
     'Fri' =&gt; 0,'Sat' =&gt; 0, 'Sun' =&gt; 0);

// Count days
foreach($data as $key =&gt; $value) {

    $dayName = date('D', strtotime($key));

    $countDays[$dayName]++;
    //add up all the qty when you go through the array the first time
   // IE all the Tuesdays in the array get added up.
    $output[$dayName] += $value;
}

// Calculate average
foreach($output as $key =&gt; $value) {
    //now take all the totals you just made in the other loop, and avg them
    $output[$key] = round($value / $countDays[$key]);
}


This is the only way I can see to improve the code. This way, you have a 'shorter' second foreach loop, which may improve the speed of your code a bit. You could also do the $value by reference and change it on the fly, but I think the way I have it now is clearer. This way you aren't doing the math more then once per array element, so you only do the avg for something like Tuesday once.

As Pinoniq stated you can drop the second foreach loop and add a function, and use array_map to do the rounding. Leave the first foreach loop the same and then add this, instead of the other foreach loop.  

function average($a, $b){
    return round($a/$b);
}
$output = array_map("average",$output,$countDays);
//var_dump($output); //&lt;-only if you want to test results.


Downside: You lose your index keys if you do it like this. However, you can easily fix that by just keeping the keys in an array, and then print them out at the same time, or use array_combine($keys,$values); to get your output array back.