Jump to content

Assigning a time to a number range


Mutley

Recommended Posts

I have some numbers:

 

$a = '26';

$b = '25.5';

$c = '24';

$d = '14';

...etc

 

What I wish to do, is for every .5 = 1 second from the highest number.

 

So, since $a is the highest number, I want it to echo something like this:

 

0s

+1s (this is $b)

+4s (this is $c)

+24s (this is $d)

 

if that makes sense?

 

Thanks. :)

Link to comment
https://forums.phpfreaks.com/topic/64080-assigning-a-time-to-a-number-range/
Share on other sites

There's probably a neater way of doing this, but I'd go with this:

 

<?php

// Array of timings to pass in to function
$times[] = "26";
$times[] = "25.5";
$times[] = "24";
$times[] = "14";

// Call to function
$secs = SecondDelay($times);

// Print out each timing difference in 'seconds'
foreach ($secs as $t){
   echo $t . "<br>";
}

// Funtion takes an unordered array of times and returns an ordered array of differences
function SecondDelay($timings){
   rsort($timings, SORT_NUMERIC);
   $highest = $timings[0];
   foreach ($timings as $time){
      $diff = floatval($highest - $time);
      $sdiff = $diff * 2;
      $seconds[] = '+' . $sdiff . 's';
   }
   return $seconds;
}

?>

 

If you want it to return the differences in the order you pass them to the function, then I can change that.

 

Hope this helps.

 

Regards

Huggie

max() will find the highest

 

<?php


// Array of timings to pass in to function
$times[] = "26";
$times[] = "25.5";
$times[] = "24";
$times[] = "14";

$max = max($times);          // get highest val in the array

foreach ($times as $k=>$t)
{
    $times[$k] = sprintf('+%ds', ($max-$t)*2);
}

echo '<pre>', print_r($times, true), '</pre>';
?>

Archived

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

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