## Posted By

mikeejay on 10/24/06

# sec2hms

/ Published in: PHP

converts seconds to h:m:s

`/** * 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	\$hours = intval(intval(\$sec) / 3600);  	// add to \$hms, with a leading 0 if asked for	if (\$hours > 0){		\$hms .= (\$useColon) 		      ? str_pad(\$hours, 2, "0", STR_PAD_LEFT). ':'		      : \$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	\$minutes = intval((\$sec / 60) % 60);  	// then add to \$hms (with a leading 0 if needed)	if (\$minutes > 0)	\$hms .= (\$useColon) 		      ? str_pad(\$minutes, 2, "0", STR_PAD_LEFT). ':'		      : \$minutes. ' min '; 	// seconds are simple - just divide the total	// seconds by 60 and keep the remainder	\$seconds = intval(\$sec % 60);  	// add to \$hms, again with a leading 0 if needed	\$hms .= (\$useColon) 		      ? str_pad(\$seconds, 2, "0", STR_PAD_LEFT)		      : \$seconds. ' sek '; 	return \$hms;}`