I need to convert seconds to \"Hour:Minute:Second\".
For example: \"685\" converted to \"00:11:25\"
How can I achieve this?
If you want to create a audio/video duration string like YouTube, etc. you can do:
($seconds >= 60) ? ltrim(gmdate("H:i:s", $seconds), ":0") : gmdate("0:s", $seconds)
Will return strings like:
55.55 => '0:55'
100 => '1:40'
Probably won't work well for time >= 24 hours.
See:
/**
* Convert number of seconds into hours, minutes and seconds
* and return an array containing those values
*
* @param integer $inputSeconds Number of seconds to parse
* @return array
*/
function secondsToTime($inputSeconds) {
$secondsInAMinute = 60;
$secondsInAnHour = 60 * $secondsInAMinute;
$secondsInADay = 24 * $secondsInAnHour;
// extract days
$days = floor($inputSeconds / $secondsInADay);
// extract hours
$hourSeconds = $inputSeconds % $secondsInADay;
$hours = floor($hourSeconds / $secondsInAnHour);
// extract minutes
$minuteSeconds = $hourSeconds % $secondsInAnHour;
$minutes = floor($minuteSeconds / $secondsInAMinute);
// extract the remaining seconds
$remainingSeconds = $minuteSeconds % $secondsInAMinute;
$seconds = ceil($remainingSeconds);
// return the final array
$obj = array(
'd' => (int) $days,
'h' => (int) $hours,
'm' => (int) $minutes,
's' => (int) $seconds,
);
return $obj;
}
From: Convert seconds into days, hours, minutes and seconds
The gmtdate() function didn't work for me as I was tracking hours worked on a project and if it's over 24 hours, you get amount left over after 24 hours is subtracted. In other words 37 hours becomes 13 hours. (all as stated above by Glavic - thanks for your examples!) This one worked well:
Convert seconds to format by 'foot' no limit :
$seconds = 8525;
$H = floor($seconds / 3600);
$i = ($seconds / 60) % 60;
$s = $seconds % 60;
echo sprintf("%02d:%02d:%02d", $H, $i, $s);
# 02:22:05
One hour is 3600sec, one minute is 60sec so why not:
<?php
$init = 685;
$hours = floor($init / 3600);
$minutes = floor(($init / 60) % 60);
$seconds = $init % 60;
echo "$hours:$minutes:$seconds";
?>
which produces:
$ php file.php
0:11:25
(I've not tested this much, so there might be errors with floor or so)
A simple way to use DateTime for this is:
$time = 60; //sec.
$now = time();
$rep = new DateTime('@'.$now);
$diff = new DateTime('@'.($now+$time));
$return = $diff->diff($rep)->format($format);
//output: 01:04:65
It's a simple solution wich gives you the ability to use the format Method of DateTime.
In java you can use this way.
private String getHmaa(long seconds) {
String string;
int hours = (int) seconds / 3600;
int remainder = (int) seconds - hours * 3600;
int mins = remainder / 60;
//remainder = remainder - mins * 60;
//int secs = remainder;
if (hours < 12 && hours > 0) {
if (mins < 10) {
string = String.valueOf((hours < 10 ? "0" + hours : hours) + ":" + (mins > 0 ? "0" + mins : "0") + " AM");
} else {
string = String.valueOf((hours < 10 ? "0" + hours : hours) + ":" + (mins > 0 ? mins : "0") + " AM");
}
} else if (hours >= 12) {
if (mins < 10) {
string = String.valueOf(((hours - 12) < 10 ? "0" + (hours - 12) : ((hours - 12) == 12 ? "0" : (hours - 12))) + ":" + (mins > 0 ? "0" + mins : "0") + ((hours - 12) == 12 ? " AM" : " PM"));
} else {
string = String.valueOf(((hours - 12) < 10 ? "0" + (hours - 12) : ((hours - 12) == 12 ? "0" : (hours - 12))) + ":" + (mins > 0 ? mins : "0") + ((hours - 12) == 12 ? " AM" : " PM"));
}
} else {
if (mins < 10) {
string = String.valueOf("0" + ":" + (mins > 0 ? "0" + mins : "0") + " AM");
} else {
string = String.valueOf("0" + ":" + (mins > 0 ? mins : "0") + " AM");
}
}
return string;
}