Solved

# (hours minutes) to seconds to (hours minutes)

Posted on 2013-05-22
280 Views
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
Question by:rgb192
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points

LVL 15

Accepted Solution

Jagadishwor Dulal earned 350 total points
ID: 39186963
If you are fetching h:i from database you can convert it to second using query like:
``````SELECT TIME_TO_SEC('05:10');
``````

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');
?>
``````
0

LVL 7

Assisted Solution

Robert Saylor earned 150 total points
ID: 39187126
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

Author Closing Comment

ID: 39188498
i like the conversion example better

thanks
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
###### Suggested Courses
Course of the Month5 days, 2 hours left to enroll