I need to convert seconds to \"Hour:Minute:Second\".
For example: \"685\" converted to \"00:11:25\"
How can I achieve this?
Other solutions use gmdate
, but fail in edge cases where you have more than 86400 seconds. To get around this, we can simply compute the number of hours ourselves, then let gmdate
compute the remaining seconds into minutes/seconds.
echo floor($seconds / 3600) . gmdate(":i:s", $seconds % 3600);
Input: 6030
Output: 1:40:30
Input: 2000006030
Output: 555557:13:50
$given = 685;
/*
* In case $given == 86400, gmdate( "H" ) will convert it into '00' i.e. midnight.
* We would need to take this into consideration, and so we will first
* check the ratio of the seconds i.e. $given:$number_of_sec_in_a_day
* and then after multiplying it by the number of hours in a day (24), we
* will just use "floor" to get the number of hours as the rest would
* be the minutes and seconds anyways.
*
* We can also have minutes and seconds combined in one variable,
* e.g. $min_sec = gmdate( "i:s", $given );
* But for versatility sake, I have taken them separately.
*/
$hours = ( $given > 86399 ) ? '0'.floor( ( $given / 86400 ) * 24 )-gmdate( "H", $given ) : gmdate("H", $given );
$min = gmdate( "i", $given );
$sec = gmdate( "s", $given );
echo $formatted_string = $hours.':'.$min.':'.$sec;
To convert it into a function:
function getHoursFormat( $given ){
$hours = ( $given > 86399 ) ? '0'.floor( ( $given / 86400 ) * 24 )-gmdate( "H", $given ) : gmdate("H", $given );
$min = gmdate( "i", $given );
$sec = gmdate( "s", $given );
$formatted_string = $hours.':'.$min.':'.$sec;
return $formatted_string;
}
If you don't like accepted answer or popular ones, then try this one
function secondsToTime($seconds_time)
{
if ($seconds_time < 24 * 60 * 60) {
return gmdate('H:i:s', $seconds_time);
} else {
$hours = floor($seconds_time / 3600);
$minutes = floor(($seconds_time - $hours * 3600) / 60);
$seconds = floor($seconds_time - ($hours * 3600) - ($minutes * 60));
return "$hours:$minutes:$seconds";
}
}
secondsToTime(108620); // 30:10:20
Solution from: https://gist.github.com/SteveJobzniak/c91a8e2426bac5cb9b0cbc1bdbc45e4b
This code avoids as much as possible of the tedious function calls and piece-by-piece string-building, and the big and bulky functions people are making for this.
It produces "1h05m00s" format and uses leading zeroes for minutes and seconds, as long as another non-zero time component precedes them.
And it skips all empty leading components to avoid giving you useless info like "0h00m01s" (instead that will show up as "1s").
Example results: "1s", "1m00s", "19m08s", "1h00m00s", "4h08m39s".
$duration = 1; // values 0 and higher are supported!
$converted = [
'hours' => floor( $duration / 3600 ),
'minutes' => floor( ( $duration / 60 ) % 60 ),
'seconds' => ( $duration % 60 )
];
$result = ltrim( sprintf( '%02dh%02dm%02ds', $converted['hours'], $converted['minutes'], $converted['seconds'] ), '0hm' );
if( $result == 's' ) { $result = '0s'; }
If you want to make the code even shorter (but less readable), you can avoid the $converted
array and instead put the values directly in the sprintf() call, as follows:
$duration = 1; // values 0 and higher are supported!
$result = ltrim( sprintf( '%02dh%02dm%02ds', floor( $duration / 3600 ), floor( ( $duration / 60 ) % 60 ), ( $duration % 60 ) ), '0hm' );
if( $result == 's' ) { $result = '0s'; }
Duration must be 0 or higher in both of the code pieces above. Negative durations are not supported. But you can handle negative durations by using the following alternative code instead:
$duration = -493; // negative values are supported!
$wasNegative = FALSE;
if( $duration < 0 ) { $wasNegative = TRUE; $duration = abs( $duration ); }
$converted = [
'hours' => floor( $duration / 3600 ),
'minutes' => floor( ( $duration / 60 ) % 60 ),
'seconds' => ( $duration % 60 )
];
$result = ltrim( sprintf( '%02dh%02dm%02ds', $converted['hours'], $converted['minutes'], $converted['seconds'] ), '0hm' );
if( $result == 's' ) { $result = '0s'; }
if( $wasNegative ) { $result = "-{$result}"; }
// $result is now "-8m13s"
Anyone whose looking for this in the future, this gives the format the initial poster asked for.
$init = 685;
$hours = floor($init / 3600);
$hrlength=strlen($hours);
if ($hrlength==1) {$hrs="0".$hours;}
else {$hrs=$hours;}
$minutes = floor(($init / 60) % 60);
$minlength=strlen($minutes);
if ($minlength==1) {$mins="0".$minutes;}
else {$mins=$minutes;}
$seconds = $init % 60;
$seclength=strlen($seconds);
if ($seclength==1) {$secs="0".$seconds;}
else {$secs=$seconds;}
echo "$hrs:$mins:$secs";
function timeToSecond($time){
$time_parts=explode(":",$time);
$seconds= ($time_parts[0]*86400) + ($time_parts[1]*3600) + ($time_parts[2]*60) + $time_parts[3] ;
return $seconds;
}
function secondToTime($time){
$seconds = $time % 60;
$seconds<10 ? "0".$seconds : $seconds;
if($seconds<10) {
$seconds="0".$seconds;
}
$time = ($time - $seconds) / 60;
$minutes = $time % 60;
if($minutes<10) {
$minutes="0".$minutes;
}
$time = ($time - $minutes) / 60;
$hours = $time % 24;
if($hours<10) {
$hours="0".$hours;
}
$days = ($time - $hours) / 24;
if($days<10) {
$days="0".$days;
}
$time_arr = array($days,$hours,$minutes,$seconds);
return implode(":",$time_arr);
}