简体   繁体   中英

How to calculate mean average of dates using PHP

I have a system that captures attendance data, which gets stored in the database with name of the person and the date of attendance. What the system has to do is to calculate the average of attendance and represent that data in the form graph.

Can someone please help on this one.

If I understand well, you have a database that records in a table the couples (name,date of attendance)

What you need to do is just count how many times each date appears. It can be done in several ways, either on the MySQL side or the PHP side.

Example with MySQL:

SELECT date,COUNT(*)  
FROM attendance       
GROUP BY date;

You will get something like:

"July 1", 18
"July 8", 22
"July 15", 16

If you want an average, this should work:

$result = mysql_query("SELECT date,COUNT(*) FROM attendance GROUP BY date;");  
while($row=mysql_fetch_array($result))  
{   
    $attendance[] = $row['COUNT(*)'];
}  
$average = array_sum($attendance) / count($attendance);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM