0

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' ");
4

2 回答 2

3

你可以这样计算,

SELECT SEC_TO_TIME( SUM( your_time_field ) ) FROM sohy_works WHERE .....
于 2013-05-28T13:38:30.213 回答
2

从 db 获取秒数并将其转换为小时和分钟:

$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;

编辑:

可以使用 SQL 查询直接计算小时和分钟,例如:

SELECT SEC_TO_TIME( SUM( TIME_TO_SEC( `worked_hours` ) ) ) FROM ...
于 2013-05-28T13:46:26.113 回答