Search code examples
phptimedigitdate-conversion

Convert Digit Time to seconds


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

Solution

  • 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;
    }