PHP Get count

Hi all,

I have records in my sql database which contains fields like ID, Name, Date
I need Name Count grouped by Month using date field.

Example :
ID | Name | Date (mm/dd/yyyy)

1 John 01/01/2013
2 David 01/20/2013
3 Harry 02/10/2013

So when i get the count based on the above data, if should be like follow

January - 2
February - 1

That is John and David falling in January Month
Harry is falling in February Month

How can i acheive this. Its like kind of excel pivot table.

Not sure if you can do this in the query, but what datatype is the date? Just a regular string, or are you using something like Date or timestamp?

I didn’t test this, but I’ll take a stab in the dark…

[php]Select COUNT(Name), MONTH(Date) from WhatEverYourTableNameIS group by MONTH(DATE) [/php]

@richei brings up a good point, I was assuming the date column, was a date data type.

Alternatively , sort the results array by month and loop through each record counting them… If you get stuck, post what you have. Really a pretty simple programming problem.

Sponsor our Newsletter | Privacy Policy | Terms of Service