1

I have a table which contains duration column that is of type time. I am using codeigniter and mysql i want that time to be summed up. So that i can display that in total seconds, hours or minutes.Please help to sort out this issue.

enter image description here

$condition = "itemid =" . "'" . $section_id . "'";
$this->db->select('*, SEC_TO_TIME(SUM(TIME_TO_SEC(`duration`))) AS timeSum ');
$this->db->from('duration');
$this->db->where($condition);
$query = $this->db->get();


foreach($query->result_array() as $row1)
{
$row['course_data'][] = $row1;
echo "time".$row1['timeSum']."<br/>";
}
user3653474
  • 3,393
  • 6
  • 49
  • 135

1 Answers1

2

Use SEC_TO_TIME to convert time into second

$this->db->select('SEC_TO_TIME( SUM( TIME_TO_SEC( `duration` ) ) ) AS timeSum '); 
$this->db->where('itemid',$id);
$query = $this->db->get('duration');
Saty
  • 22,443
  • 7
  • 33
  • 51
  • Saty thanks for your answer. I tried your code but it is showing sql syntax error on following line: $this->db->select('* SEC_TO_TIME(SUM(TIME_TO_SEC(`duration`))) AS timeSum '); – user3653474 Nov 05 '15 at 10:15
  • It was my fault i have not placed comma in select statement, but the output i get is time-00:09:15, time-00:00:00,time-01:00:00, time-00:00:00 – user3653474 Nov 05 '15 at 10:23
  • but it should output 00:09:15 i.e. 9 min 15 sec for itemid 1 – user3653474 Nov 05 '15 at 10:24
  • Check this link http://stackoverflow.com/questions/3172332/convert-seconds-to-hourminutesecond – Saty Nov 05 '15 at 10:32