Jump to content

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/273840-today-1-month/
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/273840-today-1-month/#findComment-1409232
Share on other sites

$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.

Link to comment
https://forums.phpfreaks.com/topic/273840-today-1-month/#findComment-1409278
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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