SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Addict frankdux's Avatar
    Join Date
    Apr 2004
    Location
    Ashland, North Carolina
    Posts
    267
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Calculating Miles Per Hour using PHP/MySQL

    What is a good approach for calculating miles per hour using PHP5 and MySQL4? I have visitors entering Hours, Minutes and Seconds for an activity and I am saving the info in a Time field in MySQL like 00:00:00. I'm not sure if I should use php or MySQL functions and if I should convert the field from Hours, Minutes, Seconds to Seconds only or if there is a quicker way.

  2. #2
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think I would just convert to seconds. You could either do
    (hours * 3600) + (minutes * 60) + seconds
    or use the mysql TIME_TO_SEC() function

    http://dev.mysql.com/doc/refman/5.1/...functions.html

  3. #3
    SitePoint Addict frankdux's Avatar
    Join Date
    Apr 2004
    Location
    Ashland, North Carolina
    Posts
    267
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am using MySQL 4.1.16 and so I wound up using the TIME_TO_SEC() function to get the time into seconds. From there it's easy to operate:

    $query = "SELECT miles,time,TIME_TO_SEC(time) from table_name";
    ...
    ...
    $mph = round($row[0]/($row[2]/60/60), 2)." mph";


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •