970_90

Thursday, 19 May 2016

Convert time in HH:MM:SS format to seconds

Convert time in HH:MM:SS format to seconds

Use below PHP Function to convert time(hh:mm:ss) to seconds.
function getSecondsFromHMS($time) {
    $timeArr = array_reverse(explode(":", $time));    
    $seconds = 0;
    foreach ($timeArr as $key => $value) {
        if ($key > 2)
            break;
        $seconds += pow(60, $key) * $value;
    }
    return $seconds;
}


Give few examples to use getSecondsFromHMS.
echo  getSecondsFromHMS('00:00:35'); //35

echo getSecondsFromHMS('00:35:35'); //2135

echo getSecondsFromHMS('35:35:35'); //128135

echo getSecondsFromHMS('135:35:35'); //488135



No comments:

Post a Comment