ansipants Posted June 25, 2013 Share Posted June 25, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/279533-issues-converting-time/ Share on other sites More sharing options...
ginerjm Posted June 25, 2013 Share Posted June 25, 2013 Try strtotime function to convert your varchar to a true "time" value if that helps. Quote Link to comment https://forums.phpfreaks.com/topic/279533-issues-converting-time/#findComment-1437732 Share on other sites More sharing options...
ansipants Posted June 25, 2013 Author Share Posted June 25, 2013 tysm, i got that part working, how would i do the division correctly to find the percent? Quote Link to comment https://forums.phpfreaks.com/topic/279533-issues-converting-time/#findComment-1437733 Share on other sites More sharing options...
Zane Posted June 25, 2013 Share Posted June 25, 2013 (edited) $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 June 25, 2013 by Zane Quote Link to comment https://forums.phpfreaks.com/topic/279533-issues-converting-time/#findComment-1437741 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.