PHP strtotime +1 month adding an extra month [duplicate]
Asked Answered
D

7

26

I have a simple variable that adds one month to today:

$endOfCycle = date("Y-m", strtotime("+1 month"));

Today is January 2013, so I would expect to get back 2013-02 but I'm getting 2013-03 instead. I can't figure out why it's jumping to March.

Delve answered 29/1, 2013 at 13:30 Comment(1)
see the Note in the Manual: Relative month values are calculated based on the length of months that they pass through. An example would be "+2 month 2011-11-30", which would produce "2012-01-30". This is due to November being 30 days in length, and December being 31 days in length, producing a total of 61 days.Typology
F
27

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.

Fisher answered 29/1, 2013 at 13:37 Comment(5)
Actually, you should use the 15th of the month as the base (or any other date not near either end of the month), then readjust to the 1st. It's a pain but consider that using the 1st of a month as the base then adding a month would, in some cases, place the next at the 31st of the same month rather than the 1st of the next month. Using a base in the middle of the month, you'll never get an unexpected result.Mastersinger
Failing that the developers over at PHP land could just fix their broken function and we wouldnt have to resort to any of the methods. The whole point is to take a string and convert it to a time stamp, not to converted it to a best guestimated maybe its this month next or last. Ask for next, get next, ask for last, get last etc.. but this has been a pain for many years and i see no fix coming any time soon.Onofredo
How about date('Y-m-d', strtotime('last day of next month'))? It works for me...Chambermaid
I can confirm the above works ^ last day of next monthHarakiri
hey @Chambermaid dude thanks for that,it was what i needTraherne
S
17

This should be

$endOfCycle=date('Y-m-d', strtotime("+30 days"));

strtotime

expects to be given a string containing a US English date format and will try to parse that format into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 UTC), relative to the timestamp given in now, or the current time if now is not supplied.

while

date

Returns a string formatted according to the given format string using the given integer timestamp or the current time if no timestamp is given.

See the manual pages for:

Saltire answered 29/1, 2013 at 13:34 Comment(0)
V
6

today is 29th of January, +1 month means 29th of Fabruary, but because February consists of 28 days this year, it overlaps to the next day which is March 1st

instead try

strtotime('next month')
Vermouth answered 29/1, 2013 at 13:37 Comment(0)
M
6

You can use this code to get the next month:

$ts = mktime(0, 0, 0, date("n") + 1, 1);
echo date("Y-m-d H:i:s", $ts);
echo date("n", $ts);

Assuming today is 2013-01-31 01:23:45 the above will return:

2013-02-01 00:00:00
2
Mon answered 31/1, 2013 at 12:24 Comment(0)
H
2

Maybe because its 2013-01-29 so +1 month would be 2013-02-29 which doesn't exist so it would be 2013-03-01

You could try

date('m/d/y h:i a',(strtotime('next month',strtotime(date('m/01/y')))));

from the comments on http://php.net/manual/en/function.strtotime.php

Harned answered 29/1, 2013 at 13:34 Comment(0)
S
-2
 $endOfCycle = date("Y-m", mktime(0, 0, 0, date("m", time())+1 , 15, date("m", time())));
Serial answered 29/1, 2013 at 13:41 Comment(0)
S
-3

try this:

$endOfCycle = date("Y-m", time()+2592000);

this adds 30 days, not exactly a month tough.

Serial answered 29/1, 2013 at 13:33 Comment(1)
this will only work on specific datesVermouth

© 2022 - 2024 — McMap. All rights reserved.