www.webdeveloper.com
Results 1 to 8 of 8

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

  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.
    I use (, ; : -) as I please- instead of learning the English language specification: I decided to learn Scheme and Java;

  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.
    I use (, ; : -) as I please- instead of learning the English language specification: I decided to learn Scheme and Java;

  7. #7
    Join Date
    Dec 2010
    Posts
    4

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

    I've put that code but it's not returning any results:
    query = sprintf('SELECT entrytitle, entrytext, entryurl, FROM blog
    WHERE YEAR(entrydate) = %d
    AND MONTH(entrydate) = %d
    ORDER BY entrydate DESC
    LIMIT %d, %d', $_GET['year'], $_GET['month'], $startrow, $display);

    Am I missing something?


    I've also tried the following, but there's a syntax error somewhere in here
    $query ="SELECT entrytitle, entrytext, entryurl, ";
    $query.=" DATE_FORMAT(entrydate, '%M %d, %Y ' '-' ' %l:%i%p') AS date";
    $query.=" FROM blog WHERE YEAR(entrydate) = $_GET['year'] AND MONTH(entrydate) = $_GET['month'] ORDER BY entrydate DESC LIMIT $startrow, $display";

    (Please note that when the last line reads $query.=" FROM blog ORDER BY entrydate DESC LIMIT $startrow, $display"; it works fine but I understand I need a where clause to get return the month I want)

    Please bare with me, but I'm a beginner trying to get better at this.

  8. #8
    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

"

"

X vBulletin 4.2.2 Debug Information

  • Page Generation 0.10067 seconds
  • Memory Usage 2,926KB
  • Queries Executed 15 (?)
More Information
Template Usage (34):
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_global_above_footer
  • (1)ad_global_below_navbar
  • (1)ad_global_header1
  • (1)ad_global_header2
  • (1)ad_navbar_below
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (1)ad_thread_first_post_content
  • (1)ad_thread_last_post_content
  • (1)bbcode_code
  • (2)bbcode_php
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)headinclude_bottom
  • (8)memberaction_dropdown
  • (1)navbar
  • (4)navbar_link
  • (1)navbar_moderation
  • (1)navbar_noticebit
  • (1)navbar_tabs
  • (2)option
  • (8)postbit
  • (8)postbit_onlinestatus
  • (8)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available (6):
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files (26):
  • ./showthread.php
  • ./global.php
  • ./includes/class_bootstrap.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/functions_navigation.php
  • ./includes/class_friendly_url.php
  • ./includes/class_hook.php
  • ./includes/class_bootstrap_framework.php
  • ./vb/vb.php
  • ./vb/phrase.php
  • ./includes/functions_facebook.php
  • ./includes/functions_calendar.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_notice.php
  • ./packages/vbattach/attach.php
  • ./vb/types.php
  • ./vb/cache.php
  • ./vb/cache/db.php
  • ./vb/cache/observer/db.php
  • ./vb/cache/observer.php 

Hooks Called (71):
  • init_startup
  • friendlyurl_resolve_class
  • init_startup_session_setup_start
  • database_pre_fetch_array
  • database_post_fetch_array
  • init_startup_session_setup_complete
  • global_bootstrap_init_start
  • global_bootstrap_init_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • load_show_variables
  • load_forum_show_variables
  • global_state_check
  • global_bootstrap_complete
  • global_start
  • style_fetch
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • strip_bbcode
  • friendlyurl_clean_fragment
  • friendlyurl_geturl
  • forumjump
  • cache_templates
  • cache_templates_process
  • template_register_var
  • template_render_output
  • fetch_template_start
  • fetch_template_complete
  • parse_templates
  • fetch_musername
  • notices_check_start
  • notices_noticebit
  • process_templates_complete
  • friendlyurl_redirect_canonical
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • memberaction_dropdown
  • tag_fetchbit
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • build_navigation_data
  • build_navigation_array
  • check_navigation_permission
  • process_navigation_links_start
  • process_navigation_links_complete
  • set_navigation_menu_element
  • build_navigation_menudata
  • build_navigation_listdata
  • build_navigation_list
  • set_navigation_tab_main
  • set_navigation_tab_fallback
  • navigation_tab_complete
  • fb_like_button
  • showthread_complete
  • page_templates