Jump to content

Getting data, mathing, echo-ing


Zakhary

Recommended Posts

Hello.

 

I have some "finish" time in ms. (for example 14000 [14s]). Also I have others guy time like 16789ms. I have made function to math difference between times.

 

function format_time($t) // t = miliseconds
{
$t1 = 16789;
$t2 = 14000;

$t = $t1 - $t2;
$minutes = floor($t / 60000);
$seconds = sprintf('%02d',floor(($t / 1000) % 60));
$ms = sprintf('%03d', $t % 1000);
return $minutes . ":" . $seconds . "." . $ms;
}

echo "+ ";
echo format_time($t);

 

So it will be "+ 0:02.789"

 

I still have a problem yet. I need to make this script mathing more than 1 time. I need around 20 other times to be mathed with 1st finish time and the difference to be displayed. And that's my problem.

 

Can you give me examples or clues how can I give this script values in ms? Then, it should math every value and echo it somewhere. Can you help me in it, please?

Link to comment
https://forums.phpfreaks.com/topic/256907-getting-data-mathing-echo-ing/
Share on other sites

i would store the times in a db, though here i have just supplied an array.

<?php
$times = array (
    16789,
    14000,
    15258,
    13249,
    12054,
    19985
    );

$min = min($times);

function format_time($t1, $t2) // t = miliseconds
{
    $t = $t1 - $t2;
    $minutes = floor($t / 60000);
    $seconds = sprintf('%02d',floor(($t / 1000) % 60));
    $ms = sprintf('%03d', $t % 1000);
    return $minutes . ":" . $seconds . "." . $ms;
}

foreach ($times as $time){
echo '+ '. format_time($time, $min). '<br>';
}
?>

i would store the times in a db, though here i have just supplied an array.

<?php
$times = array (
    16789,
    14000,
    15258,
    13249,
    12054,
    19985
    );

$min = min($times);

function format_time($t1, $t2) // t = miliseconds
{
    $t = $t1 - $t2;
    $minutes = floor($t / 60000);
    $seconds = sprintf('%02d',floor(($t / 1000) % 60));
    $ms = sprintf('%03d', $t % 1000);
    return $minutes . ":" . $seconds . "." . $ms;
}

foreach ($times as $time){
echo '+ '. format_time($time, $min). '<br>';
}
?>

 

Would it be possible that I can enter data into this array from some kinda admin panel input? I can use MySQL ofc. Anyway this won't work as it should. I wanted it to math difference to only 1st time that I set. Not 6th to 5th, 5th to 4th and so on.

create a form that manipulates the database. it's pretty basic.

 

as for the code - it finds the lowest time and compares every other time to that.

if that's not what you want, just replace $min with whatever value you want to compare against

 

each time is compared against min, not as you seem to think, 6->5, 5->4, etc.

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.