简体   繁体   中英

Sum total rows of hours

I have a table with a column "worked hours" and i want to sum all rows like this:

    08:00 
    02:00 
    03:30
Total: 13:30

And this is my sql select code

$sql=mysql_query("SELECT * FROM sohy_works WHERE data_raport = '$data' AND codangajat = '$codangajat' ");

你可以这样计算

SELECT SEC_TO_TIME( SUM( your_time_field ) ) FROM sohy_works WHERE .....

Get sum of seconds from db and convert that to hours and minutes:

$q = "SELECT SUM(TIME_TO_SEC(worked_hours)) FROM sohy_works WHERE data_raport = '$data' AND codangajat = '$codangajat'";

$result = mysql_query( $q );
$sec = mysql_fetch_array($result);

$hours = floor( $sec[0]/3600 );
$minutes = str_pad( floor( $sec[0] % 3600/60 ), 2, '0', STR_PAD_LEFT );

echo $hours.':'.$minutes;

EDIT:

Hours and minutes can be calculated directly with SQL query like:

SELECT SEC_TO_TIME( SUM( TIME_TO_SEC( `worked_hours` ) ) ) FROM ...

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM