MoFish Posted November 28, 2013 Share Posted November 28, 2013 Hi,Is it possible to get the last date of a month in mysql on a start date variable? Example... WHERE date >= '$date' and date <= GETLASTDAY('$date'): I'm aware this wont work - but may help understand what i'm trying to achieve.I currently pass in a variable called $date which has a value of "2013-11-01" . I'm hoping to execute a mysql query where it would figure out the last possible date in that month based on the $date variable e.g '2013-11-30'. In theory i want to execute something like the below - but it automagically figure out the second value WHERE date >= '2013-11-01' and date <= '2013-11-30': Is there an easy way to do this? Thanks, MoFish Quote Link to comment https://forums.phpfreaks.com/topic/284359-mysql-last-day-of-month/ Share on other sites More sharing options...
Solution Barand Posted November 28, 2013 Solution Share Posted November 28, 2013 (edited) $date = '2013-11-01'; $last = date('Y-m-t', strtotime($date)); echo "$date - $last"; // --> 2013-11-01 - 2013-11-30 or you could do WHERE EXTRACT(YEAR_MONTH FROM date) = EXTRACT(YEAR_MONTH FROM '$date') Edited November 28, 2013 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/284359-mysql-last-day-of-month/#findComment-1460513 Share on other sites More sharing options...
MoFish Posted November 28, 2013 Author Share Posted November 28, 2013 Thats exactly what I was looking for. Thank you Barand! Quote Link to comment https://forums.phpfreaks.com/topic/284359-mysql-last-day-of-month/#findComment-1460515 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.