jeff5656 Posted May 4, 2011 Share Posted May 4, 2011 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? :-) Quote Link to comment https://forums.phpfreaks.com/topic/235505-get-last-month/ Share on other sites More sharing options...
jeff5656 Posted May 4, 2011 Author Share Posted May 4, 2011 I tried this but got an error: $query = "select * from dos WHERE month(billing_level) = '04'"; the field is billing_level. I got this error: Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource Quote Link to comment https://forums.phpfreaks.com/topic/235505-get-last-month/#findComment-1210365 Share on other sites More sharing options...
jeff5656 Posted May 4, 2011 Author Share Posted May 4, 2011 never mind, that worked. I just had the wrong field name. Quote Link to comment https://forums.phpfreaks.com/topic/235505-get-last-month/#findComment-1210367 Share on other sites More sharing options...
mikosiko Posted May 4, 2011 Share Posted May 4, 2011 take a look to the functions DATE_ADD() and DATE_SUB(), both could help you now and in the future http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-sub Quote Link to comment https://forums.phpfreaks.com/topic/235505-get-last-month/#findComment-1210371 Share on other sites More sharing options...
PFMaBiSmAd Posted May 4, 2011 Share Posted May 4, 2011 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)) Quote Link to comment https://forums.phpfreaks.com/topic/235505-get-last-month/#findComment-1210390 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.