user6551260
user6551260

Reputation:

how to SUM hours in php

Below is my code its calculation is wrong please how can I make it correct

Controller:

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

View code:

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

I want to calculate total working hours of a particular project doesn't know where I am wrong Please Help... Thank you in Advance

Upvotes: 0

Views: 228

Answers (2)

Ghugu
Ghugu

Reputation: 489

It's pretty long but I hope it will help you

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

Upvotes: 1

Chetan Panchal
Chetan Panchal

Reputation: 391

Try looking into UNIX_TIMESTAMP and SEC_TO_TIME.

You would sum up the differences between the timestamps, then use that value (would be in milliseconds) to get the time:

please see below example

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

Upvotes: 0

Related Questions