recurring

PHP: Calculating Monthly/Yearly Billing Dates - Same day next month

On a recent project, I needed to generate the timestamp for the 'same day, next month' for billing purposes. This can be tough in some circumstances, like when a user signs up on the 31st of August. What happens in September, when there are 30 days? If I were to simply try something like strtotime('+1 month'), I would get back the first day of October rather than September 30th. Same problem happens when someone hits February (oh my! only 28 days there... sometimes 29).

After looking through a bunch of different forums, Stack Overflow, etc., and finding that most people simply pro-rated the current month and billed people on the 1st (which is a valid option), I decided to write my own function that calculates the same day, next month, and simply gives the last day of the next month if the next month doesn't have as many days as this month.

It's pretty much self-documenting. It could probably be reworked to be a little faster, but I had to do this pretty quickly, and it's pretty robust imo: