Jump to content

Select articles from within a month of todays date?


Recommended Posts

$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);

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;

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

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*

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.