Convert Digit Time to seconds

151 views Asked by At

How can I convert a digit time into seconds?

I need to compare the time so I though that it is easier to convert the digit time to seconds and compare the seconds later on.

for example:

00:00:33
00:01:33
02:01:33
4

There are 4 answers

0
Kraang Prime On BEST ANSWER

You can write a custom parser if you are looking for precise seconds from that format.

Something like this should do the trick :

echo hhmmss2seconds("18:24:35");

function hhmmss2seconds($time) {
    $digits = explode(":", $time);
    $seconds = 0;
    $seconds = $seconds + intval($digits[0]) * 3600; // hours
    $seconds = $seconds + intval($digits[1]) * 60; // minutes
    $seconds = $seconds + intval($digits[2]); // seconds    
    return $seconds;
}
0
Suchit kumar On

Try this:

echo strtotime("1970-01-01 00:00:11  UTC");
0
Ehsan Ilahi On

Try This its working

 <?php
    function time_to_seconds($time) { 
        list($h, $m, $s) = explode(':', $time); 
        return ($h * 3600) + ($m * 60) + $s; 
    }
    echo time_to_seconds("00:00:33");
    echo time_to_seconds("00:01:33");
    echo time_to_seconds("02:01:33");

    ?>

Thank you..

5
AudioBubble On

You want to use strtotime(). This converts "digit time" to Unix time.

$time_in_seconds = strtotime('00:00:33');