I need to convert seconds to \"Hour:Minute:Second\".
For example: \"685\" converted to \"00:11:25\"
How can I achieve this?
here you go
function format_time($t,$f=':') // t = seconds, f = separator
{
return sprintf("%02d%s%02d%s%02d", floor($t/3600), $f, ($t/60)%60, $f, $t%60);
}
echo format_time(685); // 00:11:25
Just in case anyone else is looking for a simple function to return this nicely formatted (I know it is not the format the OP asked for), this is what I've just come up with. Thanks to @mughal for the code this was based on.
function format_timer_result($time_in_seconds){
$time_in_seconds = ceil($time_in_seconds);
// Check for 0
if ($time_in_seconds == 0){
return 'Less than a second';
}
// Days
$days = floor($time_in_seconds / (60 * 60 * 24));
$time_in_seconds -= $days * (60 * 60 * 24);
// Hours
$hours = floor($time_in_seconds / (60 * 60));
$time_in_seconds -= $hours * (60 * 60);
// Minutes
$minutes = floor($time_in_seconds / 60);
$time_in_seconds -= $minutes * 60;
// Seconds
$seconds = floor($time_in_seconds);
// Format for return
$return = '';
if ($days > 0){
$return .= $days . ' day' . ($days == 1 ? '' : 's'). ' ';
}
if ($hours > 0){
$return .= $hours . ' hour' . ($hours == 1 ? '' : 's') . ' ';
}
if ($minutes > 0){
$return .= $minutes . ' minute' . ($minutes == 1 ? '' : 's') . ' ';
}
if ($seconds > 0){
$return .= $seconds . ' second' . ($seconds == 1 ? '' : 's') . ' ';
}
$return = trim($return);
return $return;
}
Well I needed something that would reduce seconds into hours minutes and seconds, but would exceed 24 hours, and not reduce further down into days.
Here is a simple function that works. You can probably improve it... But here it is:
function formatSeconds($seconds)
{
$hours = 0;$minutes = 0;
while($seconds >= 60){$seconds -= 60;$minutes++;}
while($minutes >= 60){$minutes -=60;$hours++;}
$hours = str_pad($hours, 2, '0', STR_PAD_LEFT);
$minutes = str_pad($minutes, 2, '0', STR_PAD_LEFT);
$seconds = str_pad($seconds, 2, '0', STR_PAD_LEFT);
return $hours.":".$minutes.":".$seconds;
}
You can use the gmdate() function:
echo gmdate("H:i:s", 685);
<?php
$time=3*3600 + 30*60;
$year=floor($time/(365*24*60*60));
$time-=$year*(365*24*60*60);
$month=floor($time/(30*24*60*60));
$time-=$month*(30*24*60*60);
$day=floor($time/(24*60*60));
$time-=$day*(24*60*60);
$hour=floor($time/(60*60));
$time-=$hour*(60*60);
$minute=floor($time/(60));
$time-=$minute*(60);
$second=floor($time);
$time-=$second;
if($year>0){
echo $year." year, ";
}
if($month>0){
echo $month." month, ";
}
if($day>0){
echo $day." day, ";
}
if($hour>0){
echo $hour." hour, ";
}
if($minute>0){
echo $minute." minute, ";
}
if($second>0){
echo $second." second, ";
}
Here is a one liner that handles negative seconds and more than 1 day worth of seconds.
sprintf("%s:%'02s:%'02s\n", intval($seconds/60/60), abs(intval(($seconds%3600) / 60)), abs($seconds%60));
For Example:
$seconds= -24*60*60 - 2*60*60 - 3*60 - 4; // minus 1 day 2 hours 3 minutes 4 seconds
echo sprintf("%s:%'02s:%'02s\n", intval($seconds/60/60), abs(intval(($seconds%3600) / 60)), abs($seconds%60));
outputs: -26:03:04