View Full Version : select distinct month from mysql datefield?
jamiller
04-10-2008, 03:23 PM
So I have a field type "date" in a mysql database. How do I select distinct months from this field in php?
Seems like it should be simple but I can't figure it out...
Thanks!
jamiller
04-10-2008, 03:48 PM
Ok, I've (kinda) figured out how to select the month: SELECT DISTINCT MONTH(dt) FROM table
but how do I display the results? In phpmyadmin when I perform the above query it says: Showing rows 0 - 0 (1 total, Query took 0.0005 sec) but shows "MONTH( dt )
4" below, which is correct as I yet only have april (4) in the database.
so confusing!
jamiller
04-10-2008, 04:07 PM
Well then. I need to thank myself! *wonders how to do that ;)
$result = mysql_query("SELECT DISTINCT MONTH(dt) AS 'Month' FROM 'table');
while($row = mysql_fetch_array($result)) {
$date = date("F", mktime(0, 0, 0, $row['Month']));
echo $date . "<br />";
}
You don't have to use the AS 'Month' part it just makes things easier if I ever need to extract year and/or date from that field again later.
Woot!
vBulletin® v3.8.1, Copyright ©2000-2012, Jelsoft Enterprises Ltd.