Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do you add hours and minutes together? 1

Status
Not open for further replies.

kaptlid

Technical User
Nov 26, 2006
86
US
I have rows from a mysql query in the format of HH:MM (I took out the seconds) with mysql time_format function)) what I would like to know is how to add all the times together?

1:50+
2:35+
10:10

=14:35

Thanks
 
there may be a neater way of doing this:

Code:
$dbhrs = 44;
$dbmins = 255;
$time = $dbhrs + floor($dbmin/60) .':'. (($dbmins % 60) * 60);
 
Code:
foreach ($timespent as $value) { 
$dbhrs[] = intval(substr($value,0,2)); 
$dbmins[] = intval(substr($value,3)); }
$dbhr = array_sum($dbhrs);
$dbmin = array_sum($dbmins); 
$time = $dbhr + floor($dbmin/60) .':'. ($dbmin % 60);
echo $time;

I am not sure why you put the divide sign after the modulus operation, but the above works.
 
there is no divide sign after the modulus operator. it is a multiplication sign.

the reason is that the modulus is based on base 10 math so you will get a decimal fraction of an hour. to make it representative of actual minutes, you need to multiply the fraction by 60.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top