Adam
Adam

Reputation: 801

strtotime skipping month when giving starting date

I'm trying to jump from month to month, starting from a specific timestamp, but when I get jump from August, September always gets skipped. Starting From August 31 (1346389200) and jumping 1 month:

strtotime('+1 month', 1346389200);

yields 1349067600 - which is October 1st. I've read all about strtotime making mistakes if it doesn't have a starting date to calculate from, but what could the issue be with this? Thanks

Upvotes: 0

Views: 358

Answers (2)

Luca Rainone
Luca Rainone

Reputation: 16458

One month after 31 August is 31 September, but, because it not exists, php force the result to 1 Oktober. So, you should force the current month on 1th day (of course if you want only year and month) :

strtotime('+1 month',strtotime(date("Y-m-1",1346389200)));

but if you use php >5.3 you can use more reliable DateTime class and methods.

Upvotes: 2

Mike Brant
Mike Brant

Reputation: 71384

You probably shouldn't use timestamps and strtotime for this comparison. You will introduce problems because of things like daylight savings, leap years, etc. Best to use DateTime and DateInterval classes/functions to do this in a more thorough manner.

http://php.net/manual/en/datetime.add.php

Upvotes: 1

Related Questions