Getting SUM by adding time in PHP

2019-07-14 23:57发布

问题:

I need a MySQL query for the following. I have a table of time duration as such below:

`hsncs_tbl`

+----+-----------------+
| Id |  time_duration  |
+----+-----------------+
| 1  | 0:12            |
 ---- -----------------
| 2  | 0:18            |
 ---- ----------------- 

Using a MySQL query, I need to be able to calculate these time duration. So from the table above I would get the answer of 0:30.

I tried this code many times, but still not working

SELECT SEC_TO_TIME(SUM(TIME_TO_SEC(time_duration))) FROM hsncs_tbl

Here's the screenshot of my code together with the output.

Related questions:

Sum of time in PHP

How to get the sum of time from database?

How to get the sum of time from database in PHP?

How to display the sum of time from database using php?

回答1:

try this one query in mysql

SELECT SEC_TO_TIME(SUM(TIME_TO_SEC(time_duration))) FROM yourTable;

hope this will help you



回答2:

You may use the following SQL Query:---

SELECT SUM(time_duration) FROM table_name;


I hope that it will work according to your requirement.



回答3:

Try this method:

`sum(time_duration) as time_duration` 

in your Mysql query.



回答4:

try this assuming time_duration is varchar column :

   select TIME_FORMAT(SEC_TO_TIME(SUM(TIME_TO_SEC(TIME_FORMAT(time_duration, '%H:%i')))),'%H:%i') time_duration 
  from table1;

DEMO

Ops: i guessed you have Hours and minutes , if you have minutes and seconds just change the format.



标签: php timer