Solved

(hours minutes) to seconds to (hours minutes)

Posted on 2013-05-22
3
284 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 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');

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 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

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

thanks
0

Featured Post

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

This article discusses how to create an extensible mechanism for linked drop downs.
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

617 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