nlafferty has asked for the wisdom of the Perl Monks concerning the following question:

I need to know how to get a total for a list of times in a while statement.
My main problem is if 60 minutes add an hour to the total hours. Probably really easy but i need a little help. Thanks.
while (@row = $sth->fetchrow()){ $total_hours = $row[4]; $total_minutes = $row[5]; print <<HTML; <TR BGCOLOR="#FFFFFF"> <TD><FONT SIZE=2 FACE=ARIAL>$print_date</FONT></TD> <TD><FONT SIZE=2 FACE=ARIAL>$row[1]</FONT></TD> <TD><FONT SIZE=2 FACE=ARIAL>$in_time</FONT></TD> <TD><FONT SIZE=2 FACE=ARIAL>$out_time</FONT></TD> <TD><FONT SIZE=2 FACE=ARIAL>$row[4]:$row[5]</FONT></TD> HTML } # End of while.

Replies are listed 'Best First'.
Re: Totaling a list of times
by tomhukins (Curate) on Oct 25, 2001 at 00:06 UTC

    I'm not entirely sure what you're trying to do, but if you want to ensure that $total_hours contains the correct amount of hours, even if $total_minutes is greater than 60, try this:

    $total_hours += int($total_minutes / 60); $total_minutes = $total_minutes % 60;

    Is this what you wanted?

      I'm going to try it. Looks like what i need though. If i can combing the two like.
      $total_time = "$total_time:$total_minutes";
      and get the right total it should work fine. Thanks
Re: Totaling a list of times
by thunders (Priest) on Oct 25, 2001 at 01:55 UTC
    you may want to take a look at perlop for an explaination of the %modulus operator. it is very useful for when you are doing date/time calculations, among other things.