views:

69

answers:

3

Hi again fellas!

Found the following snippet online and are currently using it in my web application, however it returns time like "0:5:1". I would like it to format the output like a real date, ie: 00:05:01.

Guess that there is an embarrassing quick solution to solve this. Here comes the snippet:

function getTimeDifference($start, $end) {
    $uts['start']      =   $start;
    $uts['end']        =    $end;
    if( $uts['start']!==-1 && $uts['end']!==-1 )
    {
        if( $uts['end'] >= $uts['start'] )
        {
            $diff    =    $uts['end'] - $uts['start'];
            if( $hours=intval((floor($diff/3600))) )
            $diff = $diff % 3600;
            if( $minutes=intval((floor($diff/60))) )
            $diff = $diff % 60;
            $diff    =    intval( $diff );
            return $hours . ':' . $minutes . ':' . $diff;
        }
        else
        {
            return FALSE;
        }
    }
    else
    {
        return FALSE;
    }
    return FALSE;

}

Thanks a lot!

+1  A: 

You could use str_pad() to padd the values with a "0".

return str_pad($hours, 2, "0", STR_PAD_LEFT); // turns '5' into '05'
Jonathan Sampson
+2  A: 

Try sprintf

return sprintf("%02:%02d:%02d", $hours, $minutes, $diff);
Paul Dixon
Thanks mate! It worked out perfectly!
Industrial
A: 

like

echo "Hours: " .sprintf( '%02d:%02d', $diff['hours'], $diff['minutes'] );

i see a similar function in link

http://www.gidnetwork.com/b-16.html

Haim Evgi
Already provided: Please upvote Paul's answer http://stackoverflow.com/questions/2049531/reformat-php-date-compare-function/2049563#2049563
Jonathan Sampson
Well yeah, that were pretty much where i'd got the function. Thanks
Industrial