kewpe20 Posted January 7, 2008 Share Posted January 7, 2008 A client provided me with an Excel Doc. with some very complex equations to determine a time period. I have been able to duplicate the results using PHP up to the point where Excel converts the decimal value into a period of time using a 24 hour time frame. For example I have a decimal value = 0.0612756581515 which equals 1 hour and 28 minutes in Excel. How do I convert that decimal value to 1 hour and 28 minutes , or 88 minutes, or 316800 seconds. There's got to be a way, but solving it is over my head. Thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/84939-convert-decimal-into-time/ Share on other sites More sharing options...
Ken2k7 Posted January 7, 2008 Share Posted January 7, 2008 I don't get how that decimal equals that time. ??? Quote Link to comment https://forums.phpfreaks.com/topic/84939-convert-decimal-into-time/#findComment-433097 Share on other sites More sharing options...
AndyB Posted January 7, 2008 Share Posted January 7, 2008 This is a gag, right? On this planet 88 minutes is 5280 seconds not 316800 seconds 1 day = 24 hours = 24 *60 minutes = 24*60*60 seconds 0.0612756581515 * 24 * 60 * 60 = 5280 (more or less, because the conversion factor is wrong, thanks to Windoze math) Quote Link to comment https://forums.phpfreaks.com/topic/84939-convert-decimal-into-time/#findComment-433106 Share on other sites More sharing options...
kewpe20 Posted January 8, 2008 Author Share Posted January 8, 2008 Not a gag. To see a live example copy 0.0612756581515 into a cell in MSEXCEL. Then right click the cell and select Format, then select the time format (hh:mm) Excel treats the number as a a percent of a 24 hour day. So, 6 hours = .25 12 hours = .5 18 hours = .75 and 24 hours = 1 That's the logic. I just can't get my head around how to convert it the opposite way. Meaning if someone said what is 6.1275% of a 24 hour day, how would I calculate it and return an answer in a hour:minute format. Quote Link to comment https://forums.phpfreaks.com/topic/84939-convert-decimal-into-time/#findComment-433148 Share on other sites More sharing options...
AndyB Posted January 8, 2008 Share Posted January 8, 2008 <?php $pct = 6.1275; $mins = $pct*1440/100 ; // 1440 minutes in most days $hrs = $pct*24/100; // 24 hours in most days $fake_time = intval($hrs). ":". (intval($mins) - 60*intval($hrs)); echo $pct. "% of a day equals ". $fake_time. " in hh:mm format";; ?> Quote Link to comment https://forums.phpfreaks.com/topic/84939-convert-decimal-into-time/#findComment-433161 Share on other sites More sharing options...
kenrbnsn Posted January 8, 2008 Share Posted January 8, 2008 Here's one way. Use the fact that a day contains 86400 seconds: <?php $spd = 86400; $dec = 0.0612756581515; $secs = $spd * $dec; $test = strtotime('midnight') + $secs; echo date ('H:i:s',$test) . "<br>"; ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/84939-convert-decimal-into-time/#findComment-433171 Share on other sites More sharing options...
kewpe20 Posted January 8, 2008 Author Share Posted January 8, 2008 I got the results I wanted with this code: <?php $pct = 6.1275; $mins = $pct*1440/100 ; // 1440 minutes in most days $hrs = $pct*24/100; // 24 hours in most days $fake_time = intval($hrs). ":". (intval($mins) - 60*intval($hrs)); echo $pct. "% of a day equals ". $fake_time. " in hh:mm format";; ?> The other solution returned 20:28:13 Thanks to the both of you for helping me out. Quote Link to comment https://forums.phpfreaks.com/topic/84939-convert-decimal-into-time/#findComment-433719 Share on other sites More sharing options...
kenrbnsn Posted January 8, 2008 Share Posted January 8, 2008 Which other solution? When I test my solution I get "01:28:14" as the answer. Ken Quote Link to comment https://forums.phpfreaks.com/topic/84939-convert-decimal-into-time/#findComment-433725 Share on other sites More sharing options...
kewpe20 Posted January 8, 2008 Author Share Posted January 8, 2008 Ken, When I copy and paste your code without making any edits to it, I get 20:28:13. Quote Link to comment https://forums.phpfreaks.com/topic/84939-convert-decimal-into-time/#findComment-433826 Share on other sites More sharing options...
Barand Posted January 8, 2008 Share Posted January 8, 2008 What does my condensed version of Ken's code give you, out of curiosity? I get same as Ken <?php $dec = 0.0612756581515; echo date ('H:i:s', mktime(0,0,0)+86400*$dec); ?> Quote Link to comment https://forums.phpfreaks.com/topic/84939-convert-decimal-into-time/#findComment-433873 Share on other sites More sharing options...
Barand Posted January 8, 2008 Share Posted January 8, 2008 Hmm, I was wondering if timezone affected the result, but I am GMT and Ken is GMT-5, so there goes that theory. Quote Link to comment https://forums.phpfreaks.com/topic/84939-convert-decimal-into-time/#findComment-433891 Share on other sites More sharing options...
GingerRobot Posted January 8, 2008 Share Posted January 8, 2008 I was wondering if it were something to do with strtotime(). I had a problem a while back where the code i posted worked perfectly for me, but not for whoever was asking for the help. I think at the time i assumed it was a difference in the strtotime() function in the different versions of PHP that we were running. If that were is that case (and it's certainly a big if) then i would imagine your code would work. Quote Link to comment https://forums.phpfreaks.com/topic/84939-convert-decimal-into-time/#findComment-433978 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.