SkyRanger Posted January 30, 2013 Share Posted January 30, 2013 I am having a problem with a date + month. This is the code: date('Y-m-d', strtotime("+1 month")) The output is: 2013-03-02 How would i get it just to go to the last day of the next month if there is only 28 or 30 days of the next month? Not sure how to go about getting what I need: ie: today is 2013-30-01 add month 2013-28-02 or today is 2013-28-03 add month 2013-03-31 or today is 2013-02-16 add month 2013-03-16 Is what I need possible? Quote Link to comment https://forums.phpfreaks.com/topic/273840-today-1-month/ Share on other sites More sharing options...
Jessica Posted January 30, 2013 Share Posted January 30, 2013 (edited) Instead of 'd' use 't'. http://www.php.net/manual/en/function.date.php And then add only 28 days, not 1 month. Edited January 30, 2013 by Jessica Quote Link to comment https://forums.phpfreaks.com/topic/273840-today-1-month/#findComment-1409215 Share on other sites More sharing options...
SkyRanger Posted January 30, 2013 Author Share Posted January 30, 2013 Perfect, thanks Jessica Quote Link to comment https://forums.phpfreaks.com/topic/273840-today-1-month/#findComment-1409217 Share on other sites More sharing options...
Christian F. Posted January 30, 2013 Share Posted January 30, 2013 Or you could use the relative date/time formats to their full potential: php > echo date ("Y-m-d", strtotime ("last day of previous month")); 2012-12-31 php > echo date ("Y-m-d", strtotime ("last day of next month")); 2013-02-28 As a bonus, you can also use the ± annotations in that format: php > echo date ("Y-m-d", strtotime ("last day of -2 month")); 2012-11-30 Quote Link to comment https://forums.phpfreaks.com/topic/273840-today-1-month/#findComment-1409232 Share on other sites More sharing options...
Barand Posted January 31, 2013 Share Posted January 31, 2013 $today = '2013-01-31'; $nextDate = date('Y-m-d', min(strtotime("$today + 1 month"), strtotime('last day of next month'))); echo $nextDate; The above will add a month but set the data to the last day of the next month if necessary. Quote Link to comment https://forums.phpfreaks.com/topic/273840-today-1-month/#findComment-1409278 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.