How to count the number of events that occurred on the same date and time in the CSV file using PHP

advertisements

i need to count the number of events occured in a same date and hour inside a CSV file.

I have an experimental project installed on a remote place of the city that measures temperature and umidity levels. It communicates (or should) with me every 5 minutes sending updated IP address to remote management (using a 3G connection).

I need to count the number of times it communicated with me to measure the 3G stability winthin specific climatic changes.

The log uses this format (separated by tabs):

Name            IP              DATE("d/m/Y G:i")
EXPERIMENTAL    187.118.104.121 13/08/2011 16:05
EXPERIMENTAL    187.118.104.121 13/08/2011 16:10
EXPERIMENTAL    187.118.104.121 13/08/2011 16:15
EXPERIMENTAL    187.118.104.121 14/08/2011 13:09
EXPERIMENTAL    187.118.104.121 14/08/2011 14:23

Example:

13/08 16H 3 Times
14/08 13H 1 Times
14/08 14H 1 Times


You can read a csv file with the fgetcsv() function:

$count_by_date = array();

$fd = fopen('file.csv', 'r');
while ($fd && !feof($fd)) {
    $data = fgetcsv($fd, 0, "\t");

    list($date, $time) = explode(' ', $data[3]);

    if (!isset($count_by_date[$date])) {
        $count_by_date[$date] = 0;
    }
    $count_by_date[$date] ++;
}