Results 1 to 3 of 3

Thread: select distinct month from mysql datefield?

  1. #1
    Join Date
    Jan 2007
    Location
    Charlotte, NC
    Posts
    82
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Question select distinct month from mysql datefield?

    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!

  2. #2
    Join Date
    Jan 2007
    Location
    Charlotte, NC
    Posts
    82
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default

    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!

  3. #3
    Join Date
    Jan 2007
    Location
    Charlotte, NC
    Posts
    82
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default

    Well then. I need to thank myself! *wonders how to do that

    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 />";
    }
    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!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •