Getting SUM by adding time in PHP

2019-07-14 23:55发布

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.

Screen shot

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?

标签: php timer
4条回答
叛逆
2楼-- · 2019-07-15 00:35

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楼-- · 2019-07-15 00:38

Try this method:

`sum(time_duration) as time_duration` 

in your Mysql query.

查看更多
甜甜的少女心
4楼-- · 2019-07-15 00:42

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.

查看更多
趁早两清
5楼-- · 2019-07-15 00:47

try this one query in mysql

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

hope this will help you

查看更多
登录 后发表回答