My table CalTime has a column Timespent which is of Time datatype. It stores values in HH:MM:SS format. Please suggest how can sum the values of this column.
Timespent
_______
00:07:00
01:05:00
00:49:00
I want to print the total like this 02:01:00.
I wonder if there is an alternative to this using php..
PS: I tried doing it with php, converting each column value (in while loop ) into timestamp using strtotime and then adding them and converting them back to required format as HH:MM:SS using date('H:i:s'). That does not seem to work. Code look something like this.
<?php while(...)
{
$ts += strtotime($timeCal['timespent']);
} ?>
Transform it in seconds, then sum it,then back to time:
SELECT SEC_TO_TIME( SUM( TIME_TO_SEC( `timespent` ) ) ) AS timeSum
FROM CalTime
Here is a simple functionality for this. You can loop through your values in an array to keep adding to the below code.
//Incase strtotime gives any errors
date_default_timezone_set('America/New_York');
//
$mytime1 = "00:07:00";
$mytime2 = "01:05:00";
$secs = strtotime($mytime2)-strtotime("00:00:00");
$result = date("H:i:s",strtotime($mytime1)+$secs);
Psuedocode Example in a loop.
Return mysql results in the php array
$mytime1 = $myTimeArray[0];
$result = 0;
for($i=0;i<sizeOf($myTimeArray;$i++)) {
$secs = strtotime($mytime[$i])-strtotime("00:00:00");
$result + = date("H:i:s",strtotime($mytime1)+$secs);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.