I know I'm doing this wrong - grouping dates
Posted: Thu Apr 22, 2010 3:49 am
My MySQL database has an integer field startdate which holds the dates when various courses will start. These are in epoch format. I can easily retrieve all the course details by date, but what I want to do is group dates by month. The following code snippet is really clumsey and does not achieve what I am after, but it's as close as I can get. In the first query I obtain the sequence of months. What I can't do is figure out how to get the subgroup of course details to follow the appropriate month heading. I would appreciate any help anyone can give me with this.
Code: Select all
$currentdate = date (U);
$getmonth = mysql_query ("select startdate from course WHERE startdate > $currentdate order by startdate");
while ($month = mysql_fetch_array($getmonth)){
$stopmonth = $monthtext;
extract ($month);
$monthtext = date('F Y', $startdate);
if ($stopmonth != $monthtext) {
echo "<h2>$monthtext</h2>";
}
$getdate = mysql_query ("select id, startdate, enddate from course WHERE startdate > $currentdate order by startdate");
while ($date = mysql_fetch_array($getdate)){
extract ($date);
$start = date('F', $startdate);
echo "ID: $id DATE: $start<br/>";
}}