Page 1 of 1

Days apart with strtotime

Posted: Wed Dec 15, 2010 11:11 am
by dimxasnewfrozen
I need to find how many days are apart from two timestamps.
I'm currently trying something like this which isn't working.

Code: Select all

$today = strtotime(date('m/d/Y')); 
$db_val = $my_db_time; // in strtotime format

$days_between = date("d", $today - $db_val);


Re: Days apart with strtotime

Posted: Wed Dec 15, 2010 11:22 am
by Jonah Bron
"d" gets the day of the month, not the number of days since epoch. You want this:

Code: Select all

$difference = (time() - $my_db_time) / (60 * 60 * 24);
That should work. By dividing the difference in seconds by (60 * 60 * 24), I convert the unit to days.

Re: Days apart with strtotime [solved]

Posted: Wed Dec 15, 2010 11:55 am
by dimxasnewfrozen
Nice.

That's what I was looking for. Thanks