1

I have a invoicing system that generates the next due date after the invoice is paid. My problem though is I want to generate the next invoice based on the date the last one was due, not when it was paid.

I'm familiar with adding days to the current date such as this:

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

Lets say the invoice was due on 2016-05-08 but it was paid on 2016-05-12 How would I get the system to add 30 days to my variable $dueDate which is being pulled from the database and set the next invoices due date 30 days from the prior?

Matthew Davis
  • 117
  • 1
  • 10
M David
  • 127
  • 7

2 Answers2

2

Use DateTime():

$dueDate = new DateTimeImmutable('2016-05-08');
$nextInvoice = $dueDate->modify('+30 days');
echo $nextInvoice->format('Y-m-d');
John Conde
  • 217,595
  • 99
  • 455
  • 496
  • Though next months due date would not be 2016-05-08, so could I put a variable in place of the actual date in the DateTimeImmutable? – M David May 09 '16 at 03:17
  • @MDavid yes, of course you can. `$foo='2016-05-08'; $dueDate = new DateTimeImmutable($foo);` –  May 09 '16 at 03:28
  • Sweet! Trying this now and will let you know if it worked. – M David May 09 '16 at 03:31
  • hmm, I'm not having luck with it. I keep getting errors that it can't be converted to a string while inputting into the database – M David May 09 '16 at 03:53
0

Try this:

$nextduedate = ('Y-m-d', strtotime($duedate. ' + 30 days'));

That will format your date, then add 30 days to the old due date stored in a variable.

Matthew Davis
  • 117
  • 1
  • 10