Jump to content

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

Link to comment
https://forums.phpfreaks.com/topic/84939-convert-decimal-into-time/
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)

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.

 

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

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.

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.

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.