How to calculate the difference of datetime field and now in Cakephp?

3.9k Views Asked by At

I have a datetime field in my database that contains the following information:

2015-08-04 18:59:01

I want to check the difference between that datetime field and now using Cakephp framework ?

2

There are 2 best solutions below

0
On

Calculate the difference between two dates:

$date1=date_create("2013-03-15");
$date2=date_create("2013-12-12");

$diff=date_diff($date1,$date2);

echo $diff->format("%R%a days");

Output: +272 days

The date_diff() function returns the difference between two DateTime objects.

2
On

See DateTime::diff

$date = '2015-08-04 18:59:01';
$dateTime = new DateTime($date);
$now = new DateTime();
$interval = $now->diff($dateTime);
echo $interval->format('%R%a days');

See DateInterval::format for other formatting options.

You can also get diff in seconds:

$date = '2015-08-04 18:59:01';
$dateTime = new DateTime($date);
$diff = time() - $dateTime->getTimestamp();