Page 1 of 1

Calculating trip time

Posted: Sun Jul 30, 2006 11:12 am
by mjmacarty
I am trying to take a number of calculated hours, say 2.4 hours, into standard time, i.e. 02:24. I'll take the seconds if I have to, but I can't seem to find a function that just takes a number as an argument like this and converts it to "time". Figured this is worth a shot before I try to write my own function.

Posted: Sun Jul 30, 2006 11:54 am
by RobertGonzalez
I'd search google for something like what you want. But I would also warn you that you could probably write a funtion faster than you could find one. It doesn't seem that involved.

Posted: Sun Jul 30, 2006 12:21 pm
by Burrito
you could try something like this:

UNTESTED:

Code: Select all

function fractionOfHours($time)
{
   $thtime = explode(".",$time);
   $fraction = number_format((60*$thtime[1])/10);
   return $thtime[0].":".$fraction;
}

echo fractionOfHours(2.4);

Posted: Sun Jul 30, 2006 5:45 pm
by mjmacarty
OK. Here is what I came up with. Anybody care to comment? Perhaps a simpler way to do this mathematically? This seems to work, but I don't often come up with very elegant solutions.

Code: Select all

function time_elapsed($dist, $speed){
  global $hours;
  global $minutes;
  global $seconds;
  $hours = intval($dist/$speed);
  $minutes = intval(bcmod($dist,$speed)/$speed*60);
  $seconds = (bcmod($dist,$speed)/$speed * 60 - $minutes) * 60;
  
  if ($hours< 10){
    $hours = '0'.$hours;
	} else $hours = $hours;
  
  if ($minutes< 10){
    $minutes = '0'.$minutes;
	} else $minutes = $minutes;
	
  if ($seconds< 10){
    $seconds = '0'.$seconds;
	} else $seconds = $seconds;
 }

Posted: Sun Jul 30, 2006 5:52 pm
by RobertGonzalez
Kill the elses on your rewriting of hours, minutes and seconds, they are unnecessary. Also, your function doesn't return a value. How are you going to use it?

Posted: Sun Jul 30, 2006 6:07 pm
by feyd

Code: Select all

$time = 2.4; // in hours

$hours = bcdiv($time, 1, 0);
$remainder = bcsub($time, $hours, 4);
$remainder = bcmul($remainder, 60, 4);
$minutes = bcdiv($remainder, 1, 0);
$remainder = bcsub($remainder, $minutes, 4);
$remainder = bcmul($remainder, 60, 4);
$seconds = bcdiv($remainder, 1, 0);
$remainder = bcsub($remainder, $seconds, 4);
list(,$remainder) = explode('.', $remainder);

echo sprintf('%01s:%02s:%02s.%04s', $hours, $minutes, $seconds, $remainder);

Posted: Sun Jul 30, 2006 9:38 pm
by mjmacarty
Everah wrote:Kill the elses on your rewriting of hours, minutes and seconds, they are unnecessary. Also, your function doesn't return a value. How are you going to use it?
Thanks. I was passing the variables out to an echo stmt, but it makes more sense to do this inside the function now that you mention it. That gets rid of the global variables too. Now I am going to see if I can work through the logic of what Feyd did.

Posted: Sun Jul 30, 2006 11:56 pm
by RobertGonzalez
Feyd is using the BC Math functions to perform the calculations that Burritos script was doing, then displaing it in HH:MM:SS.ss format. It is a really procedure.