PHP - 按日期显示博客菜单

时间:2011-06-19 19:08:02

标签: php mysql database blogs date

我已经创建了一个函数来显示这些日期中的帖子的日期和数量,以用作我的博客菜单,但它只是部分有效,任何帮助都会受到赞赏。

被调用的函数:

function displayBlogMenu()
{
    blogDBSelect();
    $sql = "SELECT datePosted FROM blog_entries";
    $results = mysql_query($sql);           
    while($post = mysql_fetch_array($results))
    {
        $startDate = date('Y-m-01', strtotime($post['datePosted']));
        $endDate = date('Y-m-01',  strtotime("+1 month", strtotime($post['datePosted'])));  
        $sql2 = "SELECT * FROM blog_entries WHERE datePosted >= '$startDate' AND datePosted < '$endDate'";
        $results2 = mysql_query($sql2); 
        $count = mysql_num_rows($results2);
    }                                   
    echo '<a href="">'.date('F, Y', strtotime($startDate)).' ('.$count.' Posts)</a><br>';
}

我希望函数将日期作为链接返回,例如:

June, 2011 (10 Posts)
July, 2011 (2 Posts)

1 个答案:

答案 0 :(得分:4)

看起来你可以通过一个查询

来做到这一点
SELECT Date_format(dateposted, '%M, %Y'), 
       COUNT(*) AS cc 
FROM   blog_entries 
GROUP  BY YEAR(dateposted), 
          MONTH(dateposted) 
ORDER  BY COUNT(*) DESC, 
          dateposted DESC