I have a database that looks like this: (dates are formated with strtotime)
ID Date
1 1338366170000
2 1337761370000
3 1337761370000
4 1337761370000
5 1337156570000
6 1331713370000
7 1331713370000
As you can see some entries are from the same date. I would now like to grab each date and the count how often it occurs.
So the end result would look like:
Date: 1338366170000
Count: 1
Date: 1337761370000
Count: 3
Date: 1337156570000
Count: 1
Date: 1331713370000
Count: 2
I am currently doing this to get all dates and count each date:
First I grab all rows:
$stats = $sql->query("SELECT * FROM stats_clicks");
Now, I think the problems begin when I try to count the entries with same date: I loop through all results and search for each date and count it.
foreach($sql->get() as $result){
//Sum clicks for each date
$date = $result["date"];
$stats = $sql->query("SELECT * FROM stats_clicks WHERE date = '$date'");
$count = count($sql->get());
echo "Date: $date<br>Count: $count<br><br>";
}
The output is:
Date: 1338366170000
Count: 1
Date: 1337761370000
Count: 3
Date: 1337761370000
Count: 3
Date: 1337761370000
Count: 3
Date: 1337156570000
Count: 1
Date: 1331713370000
Count: 2
Date: 1331713370000
Count: 2
How can I avoid outputing each date multiple times, but instead each date only once with the correct count?
Result