如何在PHP

时间:2017-01-11 05:33:48

标签: php

以下是我的代码,其计算错误,请问如何才能正确

控制器:

$q = 'Select sum(hours) as total_hours , client_projects.project_name  
      from client_projects join project_assignees 
      on (project_assignees.project_id = client_projects.project_id) 
      join daily_status on (daily_status.project_name = client_projects.project_name) 
      where client_projects.project_id = "'.$project_id.'" ';
$var = $this->project_model->q_single($q);

查看代码:

    <tr>
    <td>Total Hours </td>
    <td><?php $init = $working_hour['total_hours']; $hours = floor($init / 3600);
          $minutes = floor(($init / 60) % 60); $seconds = $init % 60; echo "$hours:$minutes:$seconds";?>
    </td>
    </tr>

我想计算一个特定项目的总工作时间并不知道我错在哪里 请帮忙... 提前谢谢

2 个答案:

答案 0 :(得分:1)

它很长,但我希望它会帮助你

Select  concat(floor(SUM( TIME_TO_SEC( `hours` ))/3600),':',floor(SUM( TIME_TO_SEC( `hours` ))/60)%60,':',SUM( TIME_TO_SEC( `hours` ))%60) as total_hours from tablename

答案 1 :(得分:0)

尝试查看UNIX_TIMESTAMP和SEC_TO_TIME。

您可以总结时间戳之间的差异,然后使用该值(以毫秒为单位)来获取时间:

请参阅以下示例

SELECT SEC_TO_TIME(time_milis / 1000)
FROM (
   SELECT SUM(UNIX_TIMESTAMP(date1) - UNIX_TIMESTAMP(date2)) as time_milis
   FROM table
)