Jump to content

[SOLVED] Get SUM from time


dreamwest

Recommended Posts

I need to tally the SUM from a column that has time format:

 

00:15

01:38

13:40

 

etc...

 

This works but when new time are added in seconds the sum of the column doesnt change.

 

$query = "SELECT SUM(time_length) FROM table"; 
$result = mysql_query($query) or die(mysql_error());

$row = mysql_fetch_array($result);
$total_minuets = $row['SUM(time_length)'] * 60;

 

Is it only getting the sum from the first 2 numbers - before the ":"

Link to comment
https://forums.phpfreaks.com/topic/157676-solved-get-sum-from-time/
Share on other sites

That's because numbers are only parsed until a 'stop' character is found, a non-numeric character. In your case that is the : separator.

 

You cannot directly sum numbers that are made up of two different number bases minutes:seconds.

 

You need to use the mysql TIME_TO_SEC() function to convert to seconds, then perform the SUM(), then use the mysql SEC_TO_TIME() function to convert the result back to a time value.

  • 1 month later...

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.