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; } } Link to comment https://forums.phpfreaks.com/topic/267119-converting-time-to-integer/ Share on other sites More sharing options...
scootstah Posted August 15, 2012 Share Posted August 15, 2012 strtotime EDIT: Or mktime Link to comment https://forums.phpfreaks.com/topic/267119-converting-time-to-integer/#findComment-1369590 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 Link to comment https://forums.phpfreaks.com/topic/267119-converting-time-to-integer/#findComment-1369591 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.