Select only month from timestamp in mysql query to archive articles by month
Hi. I am a beginner in MySQL and have a question. I've used the same code to return all rows in the database table. Now I want to specify criteria for the date column (timestamp) to only return articles in that specified month.
Here is my query:
$query ="SELECT entrytitle, entrytext, entryurl, ";
$query.=" DATE_FORMAT(entrydate, '%M %d, %Y ' '-' ' %l:%i%p') AS date";
$query.=" FROM blog ORDER BY entrydate DESC LIMIT $startrow, $display";
$result=mysql_query($query);
I've been researching and playing around w/ the code for about 2 days. I guess I want something along the lines of this:
$query.=" DATE_FORMAT(entrydate, '%M') AS date WHERE entrydate = $month ";
However, that doesn't seem to be working. Maybe that's complete wrong or maybe it's syntax...I don't know.
This is specified earlier in the code (in case you're wondering):
$month = $_GET["month"];
$year= $_GET["year"];
I really want to do this in MySQL query because it's suppose to be much easier than doing all the conversions in PHP.
I will greatly appreciate your time and any help given.
At this time, sorry but some reasons, I won't be able to do more ...
You stated that you want filter the date column, to get only the one in the specified month. But of what I see, you don't have any WHERE clause that reflect this requirement ...
Select only month from timestamp in mysql query to archive articles by month
I know that's where my problem is as stated in the previous post. I would someone to help me figure out what to put there. I've been working on this for a couple of days before I resorted to coming here and asking for help. If anybody could give me an example of what to put there, please help. Thanks in advance.
$query = sprintf('SELECT entrytitle, entrytext, entryurl, "that was easy" FROM blog WHERE YEAR(entrydate) = %d AND MONTH(entrydate) = %d ORDER BY entrydate DESC LIMIT %d, %d', $_GET['year'], $_GET['month'], $startrow, $display);
Last edited by eval(BadCode); 12-31-2010 at 12:34 AM.
I use (, ; : -) as I please- instead of learning the English language specification: I decided to learn Scheme and Java;
$query = "SELECT ID, Title, Category, Priority, Sticky, Author, More, Introtext,UCASE( DATE_FORMAT(Publish_Up,'%b %d, %Y %l:%i %p'))AS publishDate, Publish_Dwn, URLS FROM content WHERE Category = 1 AND Publish_Up <= DATE_ADD(NOW(),INTERVAL 1 HOUR) AND (Publish_Dwn > DATE_ADD(NOW(),INTERVAL 1 HOUR) OR Sticky=1) AND Priority > 0 ORDER BY sticky DESC, priority ASC, id DESC LIMIT ".$show;
The Publish_Up date is the date you want it to appear, it can be now or in the future.
The Publish_Dwn Date is the 1st day of next month; i.e. 2011-02-01 00:00:00
I have other entries whose Publish_Dwn date are 2 days after an event. -- An event expired.
I have a column in the table to know when to take down the article. Most come down at the end of the month. If tagged sticky they stay up until un-stickied.
Bookmarks