Calculate total subscription end date from multiple subscription end dates in PHP

213 Views Asked by At

Today is 2014-11-16.

I got these three dates in the future but I want them to be only one date instead.

2014-12-15 21:27:12
2014-12-15 21:32:20
2014-12-16 12:22:09

I want to get the total end date from today. That would be aproximately three month into the future but how can i calculate it to be the same date each time from the three dates above?

How can this be achieved with PHP's DateTime and DateInterval classes?

1

There are 1 best solutions below

0
On

This might help:

$dateTimeObject=new DateTime(date('Y-m-d h:i:s', strtotime('2014-12-16 12:22:09')));
//to get diff with now
$diff = $dateTimeObject->diff(new DateTime(date('Y-m-d h:i:s')));

Then,

//to get diff months
echo $diff->m;
//to get diff hours
echo $diff->h;
//to get diff minutes
echo $diff->i;

And so on.

It is highly recommended to take a look at the PHP's DateTime official document.

Note that, DateTime is available on PHP >=5.2.0