#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    3
    Rep Power
    0

    Dealing with Dealing with hundredths of a second.


    I'm quite new to PHP I'm developing something that requires me to list times down to a hundredth of a second. Which should be formatted like this

    Example

    1:33:42.333
    (hrs)minutes)seconds).(miliseconds)

    I'm just wondering if there is an easy way of storing a time like that in a database and an easy way of sorting it in PHP or MySQL. Or will I have to store each segment in a separate table and sort them separately?

    Thanks for your help.
  2. #2
  3. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,143
    Rep Power
    9398
    Time as in a duration (1 hour, 33 minutes, and 42.333 seconds) or time as in a specific moment (1:33:42.333 am)?

    Assuming the latter former, you can simply store it in MySQL as a FLOAT for the whole time (5622.333), which is best if the time is not precise but you do need milliseconds, or a DECIMAL(N,3) with N varying according to the data, if you need strictly three decimal places for every operation.

    How about describing what this time is for and how it will be used?
    Last edited by requinix; October 30th, 2013 at 03:08 AM. Reason: latter/former
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    3
    Rep Power
    0
    Originally Posted by requinix
    Time as in a duration (1 hour, 33 minutes, and 42.333 seconds) or time as in a specific moment (1:33:42.333 am)?

    Assuming the latter, you can simply store it in MySQL as a FLOAT for the whole time (5622.333), which is best if the time is not precise but you do need milliseconds, or a DECIMAL(N,3) with N varying according to the data, if you need strictly three decimal places for every operation.

    How about describing what this time is for and how it will be used?
    It's a list of times for a race so it's not a specific time it's the amount of time taken to get round the track.

    2 hrs 33 minutes 44 seconds 556 milliseconds
    2 hrs 33 minutes 44 seconds 557 milliseconds
    2 hrs 33 minutes 44 seconds 558 milliseconds
    ...and so on
  6. #4
  7. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,143
    Rep Power
    9398
    Then I would go for a DECIMAL(9, 3) type which allows numbers up to 999999.999 (>11 days, just for the sake of picking a limit you'll never reach). The downside to floating-point is in the representation of a number - they're not always exact - and that could cause problems when comparing multiple race times with millisecond precision.

    Store the time in milliseconds (2h33m44s556u = 9224.556) and reformat when you want to display it.
    PHP Code:
    $value_from_database 9224.556;

    $hours floor($value_from_database 3600); // 2
    $v2 $value_from_database 3600 $hours// 2024.556
    $minutes floor($v2 60); // 33
    $v3 $v2 60 $minutes// 44.556
    $seconds sprintf("%.3f"$v3); // 44.556 (but would have zero-padding) 
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    3
    Rep Power
    0
    Originally Posted by requinix
    Then I would go for a DECIMAL(9, 3) type which allows numbers up to 999999.999 (>11 days, just for the sake of picking a limit you'll never reach). The downside to floating-point is in the representation of a number - they're not always exact - and that could cause problems when comparing multiple race times with millisecond precision.

    Store the time in milliseconds (2h33m44s556u = 9224.556) and reformat when you want to display it.
    PHP Code:
    $value_from_database 9224.556;

    $hours floor($value_from_database 3600); // 2
    $v2 $value_from_database 3600 $hours// 2024.556
    $minutes floor($v2 60); // 33
    $v3 $v2 60 $minutes// 44.556
    $seconds sprintf("%.3f"$v3); // 44.556 (but would have zero-padding) 
    It needs to be exact to the hundredth of a second exactly. Will this give me that accuracy?

IMN logo majestic logo threadwatch logo seochat tools logo