Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 295
  • Last Modified:

(hours minutes) to seconds to (hours minutes)

a user is entering time in an input textbox which gets saved into mysql and then displayed in the input textbox again

hours minutes in this format:
h:i

6:05


want to convert
to seconds mysql format int

and then back into
h:i

how to do the two conversions


is there a better way to do this
0
rgb192
Asked:
rgb192
2 Solutions
 
Jagadishwor DulalBraces MediaCommented:
If you are fetching h:i from database you can convert it to second using query like:
SELECT TIME_TO_SEC('05:10');

Open in new window


To convert again to h:i:s see the function:
<?php
function convertTime($seconds)
{
    $ret = "";
    $hours = intval(intval($seconds) / 3600);
    if($hours > 0)
    {
        $ret .= "$hours:";
    }
    $minutes = bcmod((intval($seconds) / 60),60);
    if($hours > 0 || $minutes > 0)
    {
        $ret .= "$minutes:";
    }
    $seconds = bcmod(intval($seconds),60);
    $ret .= "$seconds";
    return $ret;
}
echo convertTime('18600');
?>

Open in new window

0
 
Robert SaylorSenior DeveloperCommented:
convert the time to epoch then back using the date function. Calculating epoch is the easiest way with time. It is already a base number and you can easily add/subtract. MySQL also supports formatting the date in your select string.
0
 
rgb192Author Commented:
i like the conversion example better

thanks
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now