Perad Posted June 6, 2007 Share Posted June 6, 2007 I am having a bit of trouble working out how to do this. I just want to get all articles posted within a month, unfortunately I am not having much luck does anyone know whow to do this? Quote Link to comment https://forums.phpfreaks.com/topic/54416-select-articles-from-within-a-month-of-todays-date/ Share on other sites More sharing options...
Perad Posted June 6, 2007 Author Share Posted June 6, 2007 I forgot to say that times are savedd as datetime in my db Quote Link to comment https://forums.phpfreaks.com/topic/54416-select-articles-from-within-a-month-of-todays-date/#findComment-269079 Share on other sites More sharing options...
bubblegum.anarchy Posted June 6, 2007 Share Posted June 6, 2007 $month = "2007-06-01"; // SQL date format YYYY-MM-DD $result = mysql_query($query = " SELECT * FROM articles WHERE date_created BETWEEN '{$month}' AND last_day('{$month}')") or trigger_error(mysql_error()."<PRE>".$query."</PRE>", E_USER_ERROR); Quote Link to comment https://forums.phpfreaks.com/topic/54416-select-articles-from-within-a-month-of-todays-date/#findComment-269091 Share on other sites More sharing options...
obsidian Posted June 6, 2007 Share Posted June 6, 2007 If you always want only this month's posts, you can just do this: SELECT * FROM articles WHERE MONTH(date_created) = MONTH(); If you want to get all of the articles one month from today, just do this: SELECT * FROM articles WHERE date_created BETWEEN CURDATE() AND CURDATE() + INTERVAL 1 MONTH; Quote Link to comment https://forums.phpfreaks.com/topic/54416-select-articles-from-within-a-month-of-todays-date/#findComment-269106 Share on other sites More sharing options...
bubblegum.anarchy Posted June 7, 2007 Share Posted June 7, 2007 This is a little silly and pedantic but anyways: SET @date_created = '2007-06-01'; SELECT benchmark(10000000, month(@date_created) = month(CURRENT_DATE)); # ~ 531ms SELECT benchmark(10000000, @date_created BETWEEN CURRENT_DATE AND last_day(CURRENT_DATE)); # ~ 219ms The month function appears to take some time. version = 4.1.21-community-nt Quote Link to comment https://forums.phpfreaks.com/topic/54416-select-articles-from-within-a-month-of-todays-date/#findComment-269643 Share on other sites More sharing options...
fenway Posted June 7, 2007 Share Posted June 7, 2007 To be fair, you're running the MONTH() function twice. Quote Link to comment https://forums.phpfreaks.com/topic/54416-select-articles-from-within-a-month-of-todays-date/#findComment-269935 Share on other sites More sharing options...
bubblegum.anarchy Posted June 7, 2007 Share Posted June 7, 2007 oh yeah... but take a look at this... SET @date_created = '2007-06-01'; SELECT benchmark(10000000, month(@date_created) = month(CURRENT_DATE)); # ~ 2719ms SELECT benchmark(10000000, @date_created BETWEEN last_day(CURRENT_DATE - interval 1 month) AND last_day(CURRENT_DATE)); # - 297ms I must have previously not run the SET line... *slaps head* Quote Link to comment https://forums.phpfreaks.com/topic/54416-select-articles-from-within-a-month-of-todays-date/#findComment-270331 Share on other sites More sharing options...
fenway Posted June 8, 2007 Share Posted June 8, 2007 Well, I'm not surprised, it's a lot of extraction of the month part. Quote Link to comment https://forums.phpfreaks.com/topic/54416-select-articles-from-within-a-month-of-todays-date/#findComment-271052 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.