durgaprasadcs Posted March 27, 2014 Share Posted March 27, 2014 Hi all,I have records in my sql database which contains fields like ID, Name, DateI need Name Count grouped by Month using date field.Example :ID | Name | Date (mm/dd/yyyy)-----------------------1 John 01/01/20132 David 01/20/20133 Harry 02/10/2013So when i get the count based on the above data, if should be like followJanuary - 2 February - 1That is John and David falling in January MonthHarry is falling in February MonthHow can i acheive this. Its like kind of excel pivot table. Quote Link to comment https://forums.phpfreaks.com/topic/287346-php-get-count/ Share on other sites More sharing options...
Jacques1 Posted March 27, 2014 Share Posted March 27, 2014 If the date field has a proper type (DATE, DATETIME or TIMESTAMP), you simply use the date and time functions: http://dev.mysql.com/doc/refman/5.6/en/date-and-time-functions.html If it's some hard-coded VARCHAR (looks like it), you have a problem. Since MySQL doesn't know what to do with your strings, you first have to repair your database by turning all pseudo-dates into actual dates. SELECT DATE_FORMAT(date, '%M') AS month, COUNT(*) AS name_count FROM test GROUP BY EXTRACT(MONTH FROM date) ORDER BY EXTRACT(MONTH FROM date) ASC ; Quote Link to comment https://forums.phpfreaks.com/topic/287346-php-get-count/#findComment-1474209 Share on other sites More sharing options...
cyberRobot Posted March 28, 2014 Share Posted March 28, 2014 If the date values aren't stored as a date type in the database, you could use PHP to figure out the month. For example: <?php echo date('n', strtotime('01/01/2013')); ?> More information about these functions, including examples, can be found in the PHP manual: http://www.php.net/ Once you know the month for each date, it just comes down to counting the occurrences. Quote Link to comment https://forums.phpfreaks.com/topic/287346-php-get-count/#findComment-1474302 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.