Tuesday, February 24, 2015

How to convert seconds to Hours and Minutes and seconds using php


var d = new Date();
               
$tins = 13016; // time in seconds

echo gmdate("H:i:s", tins);
print "-------------------";

$hours = floor($tins / 3600);
$minutes = floor(($tins / 60) % 60);
$seconds = $tins % 60;

echo "$hours:$minutes:$seconds";

print "------------------------------------------";
function secondsToTime($seconds)
{
    // get hours
    $hours = floor($seconds / (60 * 60));
 
    // get minutes
    $divisor_for_minutes = $seconds % (60 * 60);
    $minutes = floor($divisor_for_minutes / 60);
 
    // get seconds
    $divisor_for_seconds = $divisor_for_minutes % 60;
    $seconds = ceil($divisor_for_seconds);
 
    // return in array
    $obj = array(
        "h" => (int) $hours,
        "m" => (int) $minutes,
        "s" => (int) $seconds,
    );
    return $obj;
}

$times = secondsToTime(tins);
print $times['h'].':'.$times['m'].':'.$times['s'];