Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

(hours minutes) to seconds to (hours minutes)

Posted on 2013-05-22
3
Medium Priority
?
290 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
[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
  • Learn & ask questions
3 Comments
 
LVL 15

Accepted Solution

by:
Jagadishwor Dulal earned 1400 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');

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 7

Assisted Solution

by:Robert Saylor
Robert Saylor earned 600 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

by:rgb192
ID: 39188498
i like the conversion example better

thanks
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

688 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