Search code examples
phptimemilliseconds

Milliseconds conversion to basic time


I need to convert 1774132 to 30:42 or 30 minutes and 42 seconds or whatever the output of this is. Is there any PHP function that does this?


Solution

  • I found this online a long time ago, but I have no idea where from anymore:

    <?php
    
    function secondsToWords($seconds)
    {
        /*** return value ***/
        $ret = "";
    
        /*** get the hours ***/
        $hours = intval(intval($seconds) / 3600);
        if($hours > 0)
        {
            $ret .= "$hours hours ";
        }
        /*** get the minutes ***/
        $minutes = bcmod((intval($seconds) / 60),60);
        if($hours > 0 || $minutes > 0)
        {
            $ret .= "$minutes minutes ";
        }
    
        /*** get the seconds ***/
        $seconds = bcmod(intval($seconds),60);
        $ret .= "$seconds seconds";
    
        return $ret;
    }
    
    echo secondsToWords(time());
    ?>