我一直試圖根據發佈的年份和月份顯示來自數據庫的帖子,但無法做到這一點,因爲我已經嘗試了一切,但仍然認爲這可以在SQL QUERY中完成,並在PHP端支持。下面是我DATABSE結構Sql和php按發佈年份,日期和月份分組數據
id | title | date
-------------------------------------------------
1 | hello this is a post | 2016-01-03 11:33 AM
2 | this is what i think | 2016-01-03 10:33 PM
3 | this is it | 2017-14-06 06:33 PM
4 | Blah Blah Blah | 2017-01-03 10:33 AM
5 | is what i think | 2016-04-03 10:33 PM
現在我想選擇和分組這種方式
你在2016年1月3日
後您好,這是一個後
這就是我認爲你的
後在2017-14-06
這是它
你在2017年1月3日
等等等等等等
您的文章張貼在2017年1月3日
是我認爲的
我試着用波紋管代碼這樣
<?php
//I swear i don't know what else to do here
$db->prepare('SELECT * FROM post
GROUP BY ("")
ORDER BY date');
$db->execute();
$output = $db->getAll();
$db->free();
if(!is_null($output)){
foreach($output as $row){
$title = $row->title;
$date = $row->date;
/*
Only this path i got near but i can only display post for this month using $currentDate below.
But how can i get other once split according to date?
*/
$split = explode(" ", $date);
$currentDate = date('Y-m-d');
if($split[0] == $currentDate){
echo 'post on '.$currentDate.'<br/>'.$title;
}else{
echo 'post on '.$split[0].'<br/>'.$title;
}
?>
'2017-14-06'?在地球上,我們只有一年的十二個月。 –