Jump to content

Archived

This topic is now archived and is closed to further replies.

kewpe20

Convert decimal into time

Recommended Posts

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

Share this post


Link to post
Share on other sites

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)

Share this post


Link to post
Share on other sites

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.

 

Share this post


Link to post
Share on other sites
<?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";;
?>

Share this post


Link to post
Share on other sites

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

 

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

Which other solution? When I test my solution I get "01:28:14" as the answer.

 

Ken

Share this post


Link to post
Share on other sites

Ken,

When I copy and paste your code without making any edits to it, I get 20:28:13.

 

Share this post


Link to post
Share on other sites

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);
?>

Share this post


Link to post
Share on other sites

Hmm, I was wondering if timezone affected the result, but I am GMT and Ken is GMT-5, so there goes that theory.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

×
×
  • 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.