Page 1 of 1

need a total for time duration

Posted: Mon Feb 14, 2005 6:23 pm
by Burrito
this should be pretty simple but I'm struggling with it...it is Monday after all.

I have three different fields on my database for time:

1 - hour
2 - minutes
3 - seconds

I am looping over a query and then adding to some local page vars like such:

$hr += $db['hour'];
$min += $db['minutes'];
etc...

I then need to convert all of that into one time duration:

in other words I might have

$hr = 0
$min = 66
$sec = 102343

obviously I need to divide 102343 by 60 to get my minutes and 66 by 60 to get my hours, but I'm really struggling putting this all together...espcially since I'm getting fractions of 100 (and not 60).

any suggestions or help you can provide will be most appreciated.

thx,

Burr

Re: need a total for time duration

Posted: Mon Feb 14, 2005 7:00 pm
by timvw
burrito wrote: I have three different fields on my database for time:

1 - hour
2 - minutes
3 - seconds
Redesign your database.



burrito wrote: $hr = 0
$min = 66
$sec = 102343

obviously I need to divide 102343 by 60 to get my minutes and 66 by 60 to get my hours, but I'm really struggling putting this all together...espcially since I'm getting fractions of 100 (and not 60).
long live rule of three :)

3600 seconds = 1 hours

=> (we divide both parts by 3600

1 seconds = 1/3600 hours

=> (we multiply with 102343)

102343 * 1 seconds = 102343 * (1/3600) hours

Solved! (Same method can be used to find out how many minutes are left.



Usually, if you know there are x seconds, and you want to calculate how many hours, minutes and seconds there are it goes like this:

Code: Select all

// find out how many complete hours there are
$hours = floor($x / 3600);

// substract those seconds to know how many seconds there remain
$remainder = $x - ($hours * 3600);

// find out how many minutes there are
$minutes = floor($x / 60);

// substract those seconds to know how many seconds there remain
$remainder = $x - ($minutes * 60);

echo "hours: $hours minutes: $minutes    seconds: $seconds";

Posted: Tue Feb 15, 2005 10:45 am
by Burrito
using that sample code, wouldn't I use "$remainder" for $x to determine my minutes and then $remainder for my final count of seconds?

Edit: I think I got it working using the remainder to determine my minutes and seconds:

Code: Select all

$hrinsec = ($hrs * 3600);
$mninsec = ($mins * 60);
$totsec = ($secs + $mninsec + $hrinsec);

$hrs = floor($totsec / 3600);
$rem = $totsec - ($hrs * 3600);
$mins = floor($rem / 60);
$secs = $rem - ($mins * 60);
thx for the assist Tim