Back Home

Open Designs

Community. Driven.

    • CommentAuthordannisbet
    • CommentTimeJul 7th 2008
     
    I've been going mad over this, so maybe someone here can help me! I wrote a script to track my running progress (can check it out here) and I want to have it automatically calculate what my average time per mile is.

    I had it calculating so the format would be along the lines of 8.50 minutes, but that is not really proper time. How can I go about formatting it so it would be 8:30, or 8m 30s? I've searched around and haven't been able to find anything that looked like a proper solution.
  1.  
    do a str_replace and replace .50 with :30.... I have no idea just my best guess
    •  
      CommentAuthorgreg
    • CommentTimeJul 8th 2008 edited by greg on the 08th July 2008 at 01:52:42 EDT
     
    probably not the most elegant solution, but here's how i'd do it:

    function slightlyBetterTime($time) { // awesome function
    $time = explode(".", $time); // separate out the minutes and seconds
    $mins = $time[0]; // the minutes is the first element in the array
    $secs = round($time[1] * 0.6); // fraction of a minute is the second element,
    // multiply by 60/100 to convert it to seconds
    return "$mins:$secs"; // and return
    }
    echo slightlyBetterTime("8.50"); // take it for a test drive
    • CommentAuthordannisbet
    • CommentTimeJul 8th 2008
     
    Looks like your solution works great, greg! Thanks! bakie
    • CommentAuthorkalyan
    • CommentTimeJul 8th 2008
     
    I have an easier n simpler solution :)

    list( $mins, $secs ) = sscanf( $time, "%d.%d" ); // getting ur mins and secs easy ...
    echo "{$mins}:{$secs}";
    echo "{$mins}m {$secs}s";