我正在創建一個Web應用程序,如果用戶點擊名爲「WEEK」的鏈接,頁面會顯示所有提交的帖子那一週。還有一個選項可以查看本月提交的所有帖子。在posts表中有一個名爲post_date的列,其中日期的格式爲:YYYY-MM-DD。MySQL:從日期在當前周和當前月份中選擇數據
我的問題:我不知道如何生成本週和本月提交的帖子。
我能成功地選擇這樣提交了關於當前日期的帖子:
$today = date("Y-m-d"); // This is the format of the DATE column named post_date in the posts table.
if($_GET['when'] == "today")
{
$when = $today;
}
else if ($_GET['when'] == "week")
{
$when = // I don't know what goes here if I want to generate posts submitted this week
}
else if ($_GET['when'] == "month")
{
$when = // I don't know what goes here if I want to generate posts submitted this month
}
// I have already connected to the database and selected the table.
$query = "SELECT * FROM posts WHERE post_date = '$when'";
$result = mysql_query($query);
// After this I Generate the Posts Using WHILE loop and mysql_fetch_array
如果$ _GET [「當」]的URL中的值是‘今天’,腳本工作正常並生成「今天」提交的帖子。但我不知道如何選擇當前周和當月。我已經完成了我的功課:我已經搜索了Google,並且也搜索了StackOverflow的匹配問題,但是還有像上週的數據這樣的問題,代碼很簡單,並且不適合我的情況。例如,可以使用WEEK($today,1)
(我不知道它是否有效,但我想要生成在當前的周和月中提交的帖子)來選擇第一,第二,第三和第四周的選項。
在此先感謝。
在現實世界中,「當前周」是一個模糊的想法。對於某些人來說,本週從週日開始,到週六結束。對於其他人,則從星期一開始到星期日結束。這對你意味着什麼? –