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!
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!
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!
Well then. I need to thank myself! *wonders how to do that
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.Code:$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 />"; }
Woot!
Bookmarks