# need some help with a maths formulae..

Hello guys

so the helper turns into the helped… :-[ :-X

Ok, so i don’t have my math head on today and can’t figure this out, was hoping someone could help clear it up for me ;D

i have a row in my database that holds an integer (initial value=0).
as the user spends time on the site this is entered into table IE: 10mins = 600
this value will keep rising the more time the user spends on the site…

What i’m trying to do is display “You have spent X days X hours X mins on the site”
X being the correct value.

The formula i’m using is as follows:

[php]echo floor(\$total_time / 86400) . ’ days ';
echo floor(\$total_time / 3600) . ’ hours ‘;
echo floor(\$total_time / 60) . ’ minutes.’;
[/php]
It seemed to work fine initially (while the values where below the real life counterparts)
IE: stored value = 600 // outputs: 0 days 0 hours 10 minutes.

now the value is higher i’m getting unacceptable results:
IE: stored value= 96823 // outputs: 1 days 26 hours 1613 minutes

As you can see, 26 hours/1613 mins is not ideal so i’m lookin for either a math formula or PHP function to fix it…

Any help would be much appreciated as i just can’t count today

Red

Hi Red,

I think what you’re missing in your code is - subtract full days from \$total_time, only then count hours, then subtract full hours to calculate minutes, etc. I use this function when I need to calculate days, hours and minutes between two given dates:

[php]<?php
function date_diff(\$date_start, \$date_end) {
\$s = \$date_end-\$date_start;
\$d = intval(\$s/86400);
\$s -= \$d86400;
\$h = intval(\$s/3600);
\$s -= \$h
3600;
\$m = intval(\$s/60);
\$s -= \$m*60;
return array(“d”=>\$d,“h”=>\$h,“m”=>\$m,“s”=>\$s);
}

// usage example
\$a = date_diff(strtotime(‘1-1-2011’),time());
echo \$a[‘d’].’ days, ‘.\$a[‘h’].’ hours, ‘.\$a[‘m’].’ minutes’;

?>[/php]

…(face in hands)…

Thanks man, i knew it was something simple, but today i just can’t get my head together and the more i tried the more frustrated i got…

Your function works great - little tweak to fit my code and works just fine

Thanks again,

Red