Jump to content

Issues converting time


ansipants

Recommended Posts

I was wondering if anyone would be able to help me as I've tried everything possible but and I can't get the time to display properly. I have 2 tables in my database, one table has a column that stores the time it would take to complete the physical assignment. I have already done the code that converts the time automatically into seconds, so that part is not the issue. 

 

Now, the second table i have captures data and then adds up all of the times from a user and spits it into a column "total_time". Now, the column it stores in is a varchar which shouldn't really matter, but the time goes in like 02:15:37.5, hours:minutes:seconds.milliseconds. I'm having a very hard time getting that to convert to seconds so that I can display the % difference between the first column mentioned in the first paragraph and the second column in this paragraph. 

 

Any help would be greatly appreciated. I've been working straight with no sleep for literally 3 days now and i think i fried my brain.

Link to comment
Share on other sites

$timeVar = explode(":", "02:15:37.5");
$totalSeconds = 0;
foreach( $timeVar as $s) {
    switch( key($timeVar ) {
        case 0:  //Hours   --- 3600 secs in an hour
             $totalSeconds += ($s * 3600);
        break;
        case 1:  //Minutes -- 60 secs in a minute
             $totalSeconds += ($s *60);
        break;
        case 2:  //Seconds.milliseconds  1000 ms in a second
              list($a, $b) = each( explode( '.', $s) );
              $totalSeconds += $a + ($b / 1000)
        break;
    }
}
echo $totalSeconds . "
\n";
echo "Rounded up" . ceil( $totalSeconds ) . "
\n";
echo "Rounded down" . floor( $totalSeconds ) . "
\n";
Edited by Zane
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.