总计小时数

时间:2013-05-28 13:31:49

标签: php mysql time sum

我有一个列有“工作时间”列的表格,我希望对所有行进行求和:

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

这是我的sql选择代码

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

2 个答案:

答案 0 :(得分:3)

你可以像这样计算,

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

答案 1 :(得分: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 ...