/ Published in: PHP
converts seconds to h:m:s
Expand |
Embed | Plain Text
/** * Convert seconds to a string in this format "NN tim NN min NN sek" * If $useColon is TRUE it returns h:m:s * * * @param integer $sec * @param bool $useColon * @return string $hms */ function sec2hms($sec, $useColon = false) { // holds formatted string $hms = ""; // there are 3600 seconds in an hour, so if we // divide total seconds by 3600 and throw away // the remainder, we've got the number of hours // add to $hms, with a leading 0 if asked for if ($hours > 0){ $hms .= ($useColon) : $hours. ' tim '; }elseif ($useColon){ $hms .= '00:'; } // dividing the total seconds by 60 will give us // the number of minutes, but we're interested in // minutes past the hour: to get that, we need to // divide by 60 again and keep the remainder // then add to $hms (with a leading 0 if needed) if ($minutes > 0) $hms .= ($useColon) : $minutes. ' min '; // seconds are simple - just divide the total // seconds by 60 and keep the remainder // add to $hms, again with a leading 0 if needed $hms .= ($useColon) : $seconds. ' sek '; return $hms; }
You need to login to post a comment.
