strtotime is useful. It can handle "+1 month" to a given time. It can cause problem if you do not think well before using.


Background knowledge:

In PHP, there are some time related functions: mktimedatestrtotime. While using integer as their parameter, the integer means number of seconds since 1 Jan, 1970 00:00:00 +00:00.


Let $a = "2012-01-30"$b = strtotime($a) will return 1327954320. To check, date("Y-m-d", $b) will return 2012-01-30.

date('t', $b) will return 31 because 't' means how many days are there in the month and January have 31 days.

Now guess, how many days are there in February of 2012?
Try using PHP to get that: $c = strtotime("+1 month", $b), and what is date('t', $c)? PHP says, it is not 29, nor 28, but 31!

Of course, February of 2012 has only 29 days.

Do we wrongly use strtotime? Try last year: date('t', strtotime("+1 month", strtotime("2011-01-01"))) will give correct 28.


The functions are used correctly, and last year is correct, so the problem is...

Think about the wrong case: $a = "2012-01-30" it is the 30th of January. What is the next month? The month is February, but the function will return an integer represents the seconds since 1970, not the month itself. So the returned integer represents a date meaning one month later than 2012-01-30. What is the date? It is 30th of February, and PHP converts it to 1st of March, and then March have 31 days.


"Month" is clear, but it is not quite clear because we cannot directly teach the computer how many days are there in a month. Luckily, PHP's strtotime can handle +1 month correctly. If we do not use it correctly, we will still get wrong result:

Today is January, and one month later, is it February?