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
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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.