Jump to content

[SOLVED] Select from MySQL based on month value


jcstanley

Recommended Posts

Hi

 

What i am trying to do is select all events from a table where month is equal to the current month.

 

For example:

 

Name        Date            ....

Event 1        2007-03-01

Event 2        2007-03-28

Event 3        2007-04-14

 

The current month is March (03) so only Event 1 and Event 2 should be selected.

 

I have a variable which contains the current month value:

 

$currentmonth=date("m");

 

but i am not sure how to incorporate this into the query:

 

$query = "SELECT raceid, name, DATE_FORMAT(date, '%d %M %y') AS formatted_date, time, lowtime, lowheight FROM racing WHERE    ????????   ORDER BY 'formatted_date' ASC";

 

Any suggestions greatly appreciated

Thanks

If you only need selections from the current month, you don't even have to use a variable:

SELECT * FROM racing WHERE MONTH(date) = MONTH(CURDATE());

 

If you prefer to use the variable, just fill it in:

SELECT * FROM racing WHERE MONTH(date) = '$m';

 

See more about date and time functions in the MySQL manual.

 

Good luck!

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.