Jump to content

get last month


jeff5656

Recommended Posts

I have a date field.  How do I select all records from last month (in this example it would be all records from April).

 

Something like $query = "select * from table where date_field = last month"

 

Thank you!

 

And moderators: aren't you proud of me for posting this here instead of php coding help? :-)

Link to comment
https://forums.phpfreaks.com/topic/235505-get-last-month/
Share on other sites

The query you tried will match April of 2011, April of 2010, April of 2009,...

 

Matching dates generally requires testing the year as well -

 

SELECT * FROM dos WHERE EXTRACT(YEAR_MONTH FROM your_date_field) = EXTRACT(YEAR_MONTH FROM DATE_SUB(CURDATE(), INTERVAL 1 MONTH))

Link to comment
https://forums.phpfreaks.com/topic/235505-get-last-month/#findComment-1210390
Share on other sites

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.