PHP Date Issue strtotime() vs. date()

July 31, 2009

I ran into the following date issue in PHP: adding one month to July 31, 2009 yields August 31, 2009; but, adding 2 months to July 31, 2009 yields October 1, 2009. Tell me how that makes sense.

The real question at hand here is how long is a month?  I would tend to say that simply adding 1 to the month column would suffice, but if you are actually working with epoch times, it isn’t that simple.  The number of seconds to add varies based on what month it is.

Sample Code

echo date("Y-m-d"); //Now:  2009-07-31
echo date("Y-m-d",strtotime("+1 months")); //One month from now:  2009-08-31
echo date("Y-m-d",strtotime("+2 months")); //Two months from now:  2009-10-01
echo date("Y-m-d",strtotime("February 28th + 2 months")); //Two months from February 28th:  2009-04-28

Stay in Touch!

Subscribe to our newsletter.

Solutions Architecture

browse through our blog articles

Blog Archive