Jump to content

[SOLVED] Simple Question Regarding Time Calculation


josborne

Recommended Posts

I have am trying to build a very quick and dirty calculator that simply adds and subtracts one time value from another. Most of the code is cobbled together from other places as I am really do not work in PHP much at all.

 

The issue I am having is that I need to be able to calculate times down to milliseconds (00:45:01.123). However, the code I have come up with truncates everything after the decimal. Since I only barely undestand the code, I have been unable to detemine why this is happening.

 

Any help would be greatly appreciate.

 



if ( $op == "add" ) {

// Calculate total time
    // 0=HH, 1=MM, 2=SS
    $number1 = explode(":", $number1);
    $number2 = explode(":", $number2);  

    // Convert each time to seconds
    $number1_seconds = (($number1[0] * 3600) + ($number1[1] * 60) + ($number1[2]));
    $number2_seconds = (($number2[0] * 3600) + ($number2[1] * 60) + ($number2[2]));
    
    // Add to produce total
    $total = $number1_seconds + $number2_seconds;

// Verifying that decimals still exist - it does at this point in the code
echo $total ;
echo '<br /><br />';

    // Convert total from seconds to full time again
    $_total_time["hrs"] = floor(($total / 3600));
    $_total_time["mins"] = floor(($total - ($_total_time["hrs"] * 3600)) / 60);
    $_total_time["secs"] = floor(($total - ($_total_time["hrs"] * 3600) - ($_total_time["mins"] * 60)));


    // Prefix with 0s where necessary
    if (strlen($_total_time["hrs"]) == 1) 
      $_total_time["hrs"] = "0" . $_total_time["hrs"];
    if (strlen($_total_time["mins"]) == 1)

      $_total_time["mins"] = "0" . $_total_time["mins"];
    if (strlen($_total_time["secs"]) == 1)
      $_total_time["secs"] = "0" . $_total_time["secs"];

    // Format total time
    $total_time = $_total_time["hrs"] . ":" . $_total_time["mins"] . ":" . $_total_time["secs"];  
echo $total_time;
}



if ( $op == "sub" ) {

// Calculate total time
    // 0=HH, 1=MM, 2=SS
    $number1 = explode(":", $number1);
    $number2 = explode(":", $number2);  

    // Convert each time to seconds
    $number1_seconds = (($number1[0] * 3600) + ($number1[1] * 60) + ($number1[2]));
    $number2_seconds = (($number2[0] * 3600) + ($number2[1] * 60) + ($number2[2]));
    
    // Add to produce total
    $total = $number1_seconds - $number2_seconds;

// Verifying that decimals still exist
echo $total ;
echo '<br /><br />';

    // Convert total from seconds to full time again
    $_total_time["hrs"] = floor(($total / 3600));
    $_total_time["mins"] = floor(($total - ($_total_time["hrs"] * 3600)) / 60);
    $_total_time["secs"] = floor(($total - ($_total_time["hrs"] * 3600) - ($_total_time["mins"] * 60)));

    // Prefix with 0s where necessary
    if (strlen($_total_time["hrs"]) == 1) 
      $_total_time["hrs"] = "0" . $_total_time["hrs"];
    if (strlen($_total_time["mins"]) == 1)

      $_total_time["mins"] = "0" . $_total_time["mins"];
    if (strlen($_total_time["secs"]) == 1)
      $_total_time["secs"] = "0" . $_total_time["secs"];

    // Format total time
    $total_time = $_total_time["hrs"] . ":" . $_total_time["mins"] . ":" . $_total_time["secs"];  
echo $total_time;
}



?>

Is there really no option to edit a post here? Weird.

 

To give a little more detail:

 

in the example above - 00:44:12.581 + 00:02:32.314

 

The result I should get is 00:46:44. 895

 

But what I am getting is 00:46:44

 

Any help would really be appreciated.

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.