How to calculate datetime field difference and now in PHP?

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

2012-05-03 17:34:01

I want to check the difference between the datetime field and now:

$now = date("Y-m-d H:i:s");

I am trying to figure out how many days elapsed between the time and the time recorded in the database field.

How can i achieve this?

+5
source share
3 answers

Here is the answer:)

$date = new DateTime("2012-05-03 17:34:01");
$now = new DateTime();

echo $date->diff($now)->format("%d days, %h hours and %i minuts");
+26
source
$diff = abs(strtotime($date2) - strtotime($date1));
+5
source

date_diff :

$datetime1 = new DateTime('2009-10-11');
$datetime2 = new DateTime("now");
$interval = $datetime1->diff($datetime2);
echo $interval->format('%R%a days');
+3
source

All Articles