Days apart with strtotime

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dimxasnewfrozen
Forum Commoner
Posts: 84
Joined: Fri Oct 30, 2009 1:21 pm

Days apart with strtotime

Post 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);

User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Days apart with strtotime

Post 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.
dimxasnewfrozen
Forum Commoner
Posts: 84
Joined: Fri Oct 30, 2009 1:21 pm

Re: Days apart with strtotime [solved]

Post by dimxasnewfrozen »

Nice.

That's what I was looking for. Thanks
Post Reply