M David
M David

Reputation: 127

Getting php to properly add days to date

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?

Upvotes: 1

Views: 221

Answers (2)

Matthew Davis
Matthew Davis

Reputation: 117

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.

Upvotes: 0

John Conde
John Conde

Reputation: 219834

Use DateTime():

$dueDate = new DateTimeImmutable('2016-05-08');
$nextInvoice = $dueDate->modify('+30 days');
echo $nextInvoice->format('Y-m-d');

Upvotes: 2

Related Questions