hairynuggets hairynuggets - 1 year ago 75
PHP Question

How to calculate the difference of datetime field 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 attempting to work out how many days have passed between now and the time written to the database field.

How can I achieve this?

Answer Source

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");
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download