Jump to content

showing a month from a selection of dates.


runnerjp

Recommended Posts

hey guys,

 

Ok so i have this in my database:

id      user      event date        time created

7    Admin    2010-06-14        1282745334

8    Admin    2010-07-14          1282745334

9    Admin    2010-07-24          1282745334

 

Now what i want to do is with this out put just show

 

JUNE

JULY

 

in an output, so i could show it on my website only if there is an event posted with that month

 

View all the events in June

 

 

.... how could i do that?

 

 

 

 

SELECT * FROM table WHERE DATE_FORMAT('%m',event_date) = $month_int

01-12 for month codes.

 

If they select events from a certain month just pass in the int value of the month via PHP and display the results.

 

DATE_FORMAT fn: http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_date-format

I'm not sure where I got the idea these produced links. Anyway, the DATE_FORMAT() parameters are the date as the first parameter and the format parameter is second.

 

I would do it this way -

$start = '2009-01-01'; // start of data to look at
$end = '2010-08-25'; // end of data to look at
$query = "SELECT DISTINCT YEAR(event_date) as year, UPPER(MONTHNAME(event_date)) as month FROM your_table WHERE event_date BETWEEN '$start' AND '$end' ORDER BY event_date";
$result = mysql_query($query) or die("Query failed: $query<br />Mysql error: " . mysql_error());

$current_year = '';
while($row = mysql_fetch_assoc($result)){
        // test for a change in the year
if($current_year != $row['year']){
	echo $row['year'] . '<br />';
	$current_year = $row['year']; // remember the new year
}
//echo "<a href='?year={$row['year']}&month={$row['month']}'>{$row['month']}</a><br />"; // in case you wanted links
echo "{$row['month']}<br />";
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.