RyanMinor Posted August 15, 2012 Share Posted August 15, 2012 How would I go about converting time (could be in hours:minutes:seconds, minutes:seconds, or just seconds format) into the number of seconds? I have a function that will take in hours:minutes:seconds values but when I supply only minutes:seconds or just seconds, it fails. function convertTimeToSeconds($time) { // Check the duration input of time if (!preg_match("/^\d+(:\d{1,2})?(:\d{1,2})?$/", $time)) { throw new exception('Invalid input format for duration.'); } // Retrieve hours, minutes, and seconds $hours = substr($time, 0, -6); $minutes = substr($time, -5, 2); $seconds = substr($time, -2); $total_seconds = ($hours * 3600) + ($minutes * 60) + $seconds; // Ensure that the result is an integer if (!is_int($total_seconds)) { throw new Exception('Output format is not integer.'); } else { return $total_seconds; } } Quote Link to comment Share on other sites More sharing options...
scootstah Posted August 15, 2012 Share Posted August 15, 2012 strtotime EDIT: Or mktime Quote Link to comment Share on other sites More sharing options...
devWhiz Posted August 15, 2012 Share Posted August 15, 2012 Try http://php.net/manual/en/function.strtotime.php - CLUEL3SS Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.