mysql sort by date

how can i sort a date field by year and month and not post more then 1 of the same month iam trying to make a post archive for e blog script just dont know how to read this date 2011/3/28 and make it out put like 3 2011 then 4 2011 and so on

Hi there,

In your SQL statement, just put ORDER BY field ASC:

SELECT `name` FROM `thetable` WHERE `id`= '2' ORDER BY `date` ASC

And when you loop through your results in the PHP (your method may be different):
[php]$dates = array();
while(($row = mysql_fetch_assoc($query)) !== false)
$year = date(“Y”,$row[‘date’]);
$month = date(“n”,$row[‘date’]);
$dates[$year][] = $month;

foreach($dates as $year => $months)
$months = array_unique($months);
foreach($months as $month)
echo $month."/".$year;

Try that out, you will of course need to adapt for your SQL and method of calling the query results etc, but let me know if this helps you out!

Sponsor our Newsletter | Privacy Policy | Terms of Service