0

I have a lot of dates stored in the database like so:

2017-06-01

And there are different months worth of data in the database. I want to show this in a dropdown menu so that a user would see:

April 2017 May 2017 June 2017

I tried this in a while loop as a start to try get the month alone:

$month = date('F', strtotime($row['b_date']));

but it just shows January 7 times.

$stmt = $link->prepare("SELECT 'b_date' FROM `summary`");
    $stmt->execute();
    $result = $stmt->get_result();
    $numRows = $result->num_rows;
    if($numRows > 0) {
        while($row = $result->fetch_assoc()) {
            $month = date('F', strtotime($row['b_date']));
            echo "<option value=''>{$month}</option>";
        }
    }
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
Iggy's Pop
  • 589
  • 1
  • 6
  • 24

1 Answers1

0

You can try something like this in your query

SELECT b_date 
FROM summary
GROUP BY YEAR(b_date), MONTH(b_date)

Similar to this issue SQL group dates by month. My guess would be you are using MySQL, here are the docs for date manipulation

Jeremy Hamm
  • 499
  • 1
  • 5
  • 14