Solved

(hours minutes) to seconds to (hours minutes)

Posted on 2013-05-22
3
270 Views
Last Modified: 2013-05-22
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
Comment
Question by:rgb192
3 Comments
 
LVL 15

Accepted Solution

by:
Jagadishwor Dulal earned 350 total points
Comment Utility
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
 
LVL 6

Assisted Solution

by:Robert Saylor
Robert Saylor earned 150 total points
Comment Utility
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

by:rgb192
Comment Utility
i like the conversion example better

thanks
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
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 …

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now