Question

Im trying to get the DISTINCT month and year from the DB. Format the dates and put them into a drop down list. But im getting all the records as being Jan 14

Heres my code:

            <select name="month">
                <?php
                    $sSDate = "SELECT DISTINCT year(eventStartDate), month(eventStartDate) FROM te_events GROUP BY eventStartDate ORDER BY eventStartDate ASC";
                    $qrSDate = mysql_query($sSDate) or die (mysql_error());
                    while($rowSDate = mysql_fetch_assoc($qrSDate))
                    {
                        $s_event_start_month = $rowSDate ['month(eventStartDate)'];
                        $s_event_start_year = $rowSDate ['year(eventStartDate)'];

                        $date = date("M", strtotime($s_event_start_month)). date("y", strtotime($s_event_start_year));

                        echo '
                        <option value="'.$date.'">
                            '.$date.'
                        </option>
                        ';
                    }
                ?>
            </select>
Was it helpful?

Solution

i don't know if i fully understand the problem but please try this:

$date = date("M y", strtotime($s_event_start_month.' '.$s_event_start_year));

or

$date = date("M y", mktime(0, 0, 0, $s_event_start_month, 1, $s_event_start_year));

about mktime

OTHER TIPS

The problem is your query:

SELECT DISTINCT year(eventStartDate)
, month(eventStartDate) 
FROM te_events 
GROUP BY eventStartDate 
ORDER BY eventStartDate ASC

Since distinct years and months are separate fields, you are getting more combinations than you want. If you want distinct year-months, which makes more sense, you want something like this:

select distinct to_char(eventStartDate, 'yyyy-mm') ym
from te_events
where whatever, maybe
order by ym

Your question does not specify the database engine, so for the sake of this answer, I picked oracle. Different databases have different ways of achieving the same result.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top