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

Link to comment
Share on other sites

  • 1 month later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.