I am creating a web app where if a user clicks on Link called “WEEK”, the page shows up all the posts which were submitted in that week. There’s also an option to view all the posts submitted this month. There’s a column in the posts table named post_date in which the date is stored in the format : YYYY-MM-DD.
My Problem: I don’t know how to generate the posts submitted this week and month.
I can successfully select the posts submitted on the current date like this:
$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
If the $_GET[‘when’]’s value in the URL is “today”, the SCRIPT works fine and generates the posts submitted ‘today’. But I don’t know how to select the current week and the current month. I have already done my homework: I have googled and also searched StackOverflow for matching questions but there were questions like data from Last Week and the code was straight forward and couldn’t fit in my situation. For example, there was an option to select the 1st, 2nd, 3rd and 4th week by using WEEK($today,1) ( I don’t know if it works or not, but I want to generate the posts submitted in the current week and month ).
Thanks in advance.
There are several MySQL functions that do things like this for you