You are invited to Log in or Register a free Frihost Account!

# Storing minutes and seconds in a database

richard270384
Gday,

I need to have a script and database which is capable of storing a user inputed period of time in minutes and seconds: E.g. 3 minutes 20 seconds.

I then need to be able to add together a series of those user inputed times to give me a total time: E.g. 3minutes 20 seconds + 2 minutes 10 seconds + 3 mins 30 seconds = 9 minutes 0 seconds.

I plan to do it as follows:

1. User enters number of minutes into a form field and number of seconds into another form field (E.g. 3 into the minutes field and 20 into the seconds field).

2. Script converts this into number of seconds and stores result in database as integer (E.g. (3 x 60) + 20 = 200 seconds stored in database).

3. on retrieval, this is either a) converted back to minutes and seconds (E.g. 200 is divided by 60 giving 3.333 - not sure what the easiest way to seperate the 3 and the .333 is to make that work) or b) it is added to another set of integers and then divided by 60 to give a total period of time (E.g. (200 + 130 + 210) / 60).

I haven't seen any way to add minutes together in php.

Is there an easier way to do this?

Does anybody have any tips for me?

Cheers,
Richard
mathiaus
working from your first method, I can see 2 possible methods.
1)Explode your answer to get 3 as your minutes and 3333 as the second part of the array. Divide this second part by 10, 100 or however many digits you have so that you have 0.3333. Times this by 60 and round up.
I'm not sure how accurate this is though, especially for larger values.

2) The way I would do it is...
Ignore everything past the decimal point. Take the 3 (or whatever number) and *60. then take this number away from your stored value (200)

I don't work with times much though, so these probably aren't the 'best' ways to achieve what you want, the latter should generally work though
snowboardalliance
 mathiaus wrote: working from your first method, I can see 2 possible methods. 1)Explode your answer to get 3 as your minutes and 3333 as the second part of the array. Divide this second part by 10, 100 or however many digits you have so that you have 0.3333. Times this by 60 and round up. I'm not sure how accurate this is though, especially for larger values. 2) The way I would do it is... Ignore everything past the decimal point. Take the 3 (or whatever number) and *60. then take this number away from your stored value (200) I don't work with times much though, so these probably aren't the 'best' ways to achieve what you want, the latter should generally work though
\

Yeah, just convert the 3.3333 to an integer, multiply by 60 and subtract as he said. Probably the easiest way.
richard270384
Subtraction... A Very good idea. Thanks fellas.
imagefree
 Quote: The way I would do it is... Ignore everything past the decimal point. Take the 3 (or whatever number) and *60. then take this number away from your stored value (200)

WOW Perfect Idea. i was also going to tell you a similat way but little bit complex. The above is easy and involves just arithmatic like:

 Code: \$time_variable= \$time_coming_from_database; //value in seconds. \$minutes_variable=round(\$time_variable/60)*60 ; \$seconds_variable=\$time_variable-\$minutes_variable; echo \$time_variable . "Equals " . \$minutes_variable . " minutes Plus" . \$seconds_variable . " seconds; // here is the output.
richard270384
For anyone who wants to know, I actually ended up doing this slightly different to the code given:

 Code: \$time_variable= \$time_coming_from_database; //value in seconds. \$minutes_variable=floor(\$time_variable/60) ; \$seconds_variable=\$time_variable-(\$minutes_variable*60); echo \$time_variable . "Equals " . \$minutes_variable . " minutes Plus" . \$seconds_variable . " seconds; // here is the output.

With this line:

 Code: \$minutes_variable=floor(\$time_variable/60) ;

The round() function was rounding up, so for example, a \$time_variable value of 169 was being divided by 60 giving 2.5xxx, and was rounded up to 3 (thus giving negative seconds after the next line of coded was executed). Floor will always round down so works better here. I also removed the *60 multiplier as that was making the minutes figure wrong in the output.

 Code: \$seconds_variable=\$time_variable-(\$minutes_variable*60);

I moved the *60 multiplier to this line so that it didnt interfere with the output of the minutes figure.

Just thought I'd add this in case anybody looks at this post in the future.

Cheers,
Richard