www.webdeveloper.com
Results 1 to 8 of 8

Thread: Select only month from timestamp in mysql query to archive articles by month

Hybrid View

  1. #1
    Join Date
    Dec 2010
    Posts
    4

    Cool 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.

  2. #2
    Join Date
    Jul 2007
    Location
    Québec
    Posts
    61
    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 ...

    Maybe you should rethink the:
    Code:
    $query.=" DATE_FORMAT(entrydate, '%M %d, %Y ' '-' ' %l:%i%p') AS date";
    And put it in a WHERE clause in your query, that will fit your needs ...

  3. #3
    Join Date
    Dec 2010
    Posts
    4

    Question 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.

  4. #4
    Join Date
    Jul 2010
    Location
    /ramdisk/
    Posts
    865
    PHP Code:

    $display 
    = (abs((int)$display 0)) ? abs((int)$display) : 25;

    $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 01:34 AM.

  5. #5
    Join Date
    Dec 2010
    Posts
    4
    Thanks for the response! Let me try that right now.

  6. #6
    Join Date
    Jul 2010
    Location
    /ramdisk/
    Posts
    865
    Forget the limit offset and hard code it as 0.

    Unless there's some good reason to be changing it; it makes no sense and it's 1 more thing to break.

    edit: nevermind, you don't display all of the blog posts on 1 page.
    Last edited by eval(BadCode); 12-31-2010 at 02:10 AM.

  7. #7
    Join Date
    Jan 2006
    Location
    MN
    Posts
    440
    This is what I do in my blog;

    PHP Code:
    $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.
    Last edited by Dasher; 01-03-2011 at 11:36 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles