我正在我的网站上创建一个档案导航,并找到了一些关于如何操作的帖子,并将一些代码汇总在一起,但似乎无法让它工作。
我希望它显示如下:
January 2015 (9)
December 2014 (8)
November 2014 (10)
October 2014 (15)
...
以下是代码:
$sql = "SELECT YEAR(timestamp_published) AS 'year', Month(timestamp_published) AS 'month', COUNT(id) AS 'count' FROM table GROUP BY YEAR(timestamp_published), MONTH(timestamp_published) DESC";
$data = $db->query($sql);
while($row = $db->fetch_assoc()) {
$data[$row['year']][$row['month']] = $row['count'];
}
foreach ($data as $year => $months) {
echo archive_date($month).' ';
foreach ($months as $month => $count) {
echo $year.' ('.$count.') (' . $data->id . ')<br/>';
}
}
}
谢谢!我最终解决了自己的问题。我在课堂上构建这个,并且有几个问题。我现在已经开始使用这些代码了,未来的任何人都可以使用。
$sql = "SELECT YEAR(timestamp_published) AS 'year', MONTH(timestamp_published) AS 'month', COUNT(`publish`) AS 'count' FROM ".static::$table_name." GROUP BY YEAR(timestamp_published) DESC, MONTH(timestamp_published) ASC";
$result = $db->query($sql);
$data = array();
while($row = $result->fetch_assoc()) {
$data[$row['year']][$row['month']] = $row['count'];
}
foreach ($data as $year => $months) {
echo '<strong>'.$year.'</strong><br/><br/>';
foreach ($months as $month => $count) {
$dateObj = DateTime::createFromFormat('!m', $month);
$monthName = $dateObj->format('F'); // March
echo $monthName . ' ('.$count.')<br/>';
}
echo '<hr/>';
}
它实际上看起来像这样:
2015年
1月(3)
二月(5)
三月(6)
......
2014年
1月(7)
二月(9)
...
答案 0 :(得分:0)
我认为你正在寻找月份的字符串表示(有点不清楚),但这可以通过使用date()
来实现
date ("F", $month)
所以在你的情况下
echo date ("F", $month) . ' ' . $year.' ('.$count.')<br/>';