PHP Strtotime +1 Month Adding an Extra Month

PHP strtotime +1 month adding an extra month

It's jumping to March because today is 29th Jan, and adding a month gives 29th Feb, which doesn't exist, so it's moving to the next valid date.

This will happen on the 31st of a lot of months as well, but is obviously more noticable in the case of January to Feburary because Feb is shorter.

If you're not interested in the day of month and just want it to give the next month, you should specify the input date as the first of the current month. This will always give you the correct answer if you add a month.

For the same reason, if you want to always get the last day of the next month, you should start by calculating the first of the month after the one you want, and subtracting a day.

increment date by one month

$time = strtotime("2010.12.11");
$final = date("Y-m-d", strtotime("+1 month", $time));

// Finally you will have the date you're looking for.

PHP Strtotime -1month -2month

It might be related to bug #44073

You could try with something like this :

echo date("M", strtotime("-3 month", strtotime(date("F") . "1")) ) . "\n";
echo date("M", strtotime("-2 month", strtotime(date("F") . "1")) ) . "\n";
echo date("M", strtotime("-1 month", strtotime(date("F") . "1")) ) . "\n";
echo date("M", time()) . "\n";

(Solution found in the comments section of strtotime ; direct link)

And the output :

Apr
May
Jun
Jul

Kind of "cheating" with the date format and month's name and all that...

Behaviour of strtotime(+1 month) and December/next year

strtotime('+1 month') add the 1 month in current month, for example

if current date is 2018-06-28 then

echo date('Y-m-d', strtotime('+1 month'));

Output is :

2018-07-28

If you want first date of month then use :

echo date('Y-m-01', strtotime('+1 month')); // +1 month (Jun to July)

or

echo date('Y-m-01'); // Return Current Month

If current month is December and you add 1 month with strtotime('+1 month') then your date will be in next year

How to add months to a particular date

You have a Unix timestamp, not an actual date. Here I use the DateTime class to create a datetime object using that Unix timestamp. Then I can add a month to it and format the output.

$date = new DateTime('@'.$student->date);
$date->modify('+1 month');
echo $date-format('jS F Y');

If you want to stick to using date() and strtotime() you would use this:

echo date("jS F Y", strtotime("+1 month", $student->date));

strtotime() would take the starting date as the second parameter and then how you wish to modify it as your first parameter.

Adding three months to a date in PHP

Change it to this will give you the expected format:

$effectiveDate = date('Y-m-d', strtotime("+3 months", strtotime($effectiveDate)));

`strtotime` bug when using + 1 month from January

You're trying to add one month to the date 2013-01-31. It should give 31th Feburary 2013, but since the date doesn't exist, it moves on to the next valid month (which is March).

You can use the following work-around:

$current = date('n', strtotime("first day of next month",strtotime($start)));

Using DateTime class:

$date = new DateTime('2013-01-31');
$date->modify('first day of next month');
echo $date->format('n');

This will correctly output 2.

Demo!

PHP add 1 month to date

simple method:

$next_month = strtotime('august 2012 next month');

better method:

$d = new Date('August 2012');
$next_month = $d->add(new DateInterval('P1M'));

relevant docs: strtotime date dateinterval



Related Topics



Leave a reply



Submit