PHP Function to Format an Integer as HH:MM:SS

Here’s a function (plus a couple of shortcut functions) that will let you format an integer as HH:MM:SS format using PHP. All you have to do is input the number of seconds and/or minutes and/or hours (it accepts decimal values for any of these), specify the precision you’d like to use, and go. You can also use this function to get from hours to minutes and seconds, or the other way around.

The logic behind the function is a little trickier than you’d imagine because of the preceding zeroes. I wrote this for myself at least half a dozen times before finally realizing that I should make a function out of it and share with everyone else. If you have any suggestions or questions, please leave them in the comments below.

<?php

//////////////////////////////
//
// Ink Plant HMS Functions
// https://inkplant.com/code/format-hms
// copyright (c) 2013-2018
//
//////////////////////////////

function hms($args=array()) {
	$defaults = array(
		'hours'=>0, //incoming hours
		'minutes'=>0, //incoming minutes
		'seconds'=>0, //incoming seconds
		'precision'=>0, //outgoing precision: "minutes" for minutes (no seconds) or an integer for precision of seconds
		'return'=>null, //what to return; if null, everything is returned in an array
	);
	foreach ($defaults as $key => $default) {
		if (array_key_exists($key,$args)) { $$key = $args[$key]; } else { $$key = $default; }
	}	

	$hours = abs($hours);
	$minutes = abs($minutes);
	$seconds = abs($seconds);
	$total_seconds = ($hours * 3600) + ($minutes * 60) + $seconds;
	unset($hours,$minutes,$seconds); //to prevent confusion

	if ($precision === 'minutes') { $total_seconds = round($total_seconds/60) * 60; }
	elseif ($precision === 0) { $total_seconds = round($total_seconds); }
	elseif ($precision !== false) { $total_seconds = round($total_seconds,$precision); }

	//calculate years, days, hours, minutes, seconds
	$remaining_seconds = $total_seconds;
	$years = floor($remaining_seconds / 31557600); //365.25 days
	$remaining_seconds = $remaining_seconds - ($years * 31557600);
	$days = floor($remaining_seconds / 86400);
	$remaining_seconds = $remaining_seconds - ($days * 86400);
	$hours = floor($remaining_seconds / 3600);
	$remaining_seconds = $remaining_seconds - ($hours * 3600);
	$minutes = floor($remaining_seconds / 60);
	$seconds = $remaining_seconds - ($minutes * 60);
	unset($remaining_seconds);

	//start return array (if we're returning an array)
	if (!$return) {
		$hms = array('total_seconds'=>$total_seconds,'years'=>$years,'days'=>$days,'hours'=>$hours,'minutes'=>$minutes,'seconds'=>$seconds);
	}

	//if text, and value is over 24 hours, switch to shorttext
	if (($return == 'text') && ($days > 0)) { $return = 'shorttext'; }

	//text (h:mm:ss)
	if ((!$return) || ($return == 'text')) {
		if ($hours > 0) {
			$hh = $hours;
			$text = $hours.':';
		} else {
			$hh = '';
			$text = '';
		}
		if (($minutes >= 10) || ($hours == 0)) { $mm = $minutes; }
		elseif ($hours > 0) { $mm = '0'.$minutes; }
		if (($minutes > 0) || ($hours > 0)) {
			$text .= $mm.':';
		} else {
			$mm = '';
		}
		if ((($minutes > 0) || ($hours > 0)) && ($seconds == 0)) { $ss = '00'; }
		elseif (($seconds >= 10) || ($minutes < 1)) { $ss = $seconds; }
		else { $ss = '0'.$seconds; }
		$text .= $ss;

		if ($return == 'text') { return $text; }
		else {
			$hms['text'] = $text;
			$hms['hh'] = $hh; //blank (instead of "0") if no hours
			$hms['mm'] = $mm; //includes preceding zero so hh:mm:ss looks right, blank if no hours and no minutes
			$hms['ss'] = $ss; //includes preceding zero so hh:mm:ss looks right
		}
	}

	//long text (comma separated numbers and words)
	if ((!$return) || ($return == 'longtext')) {
		$longtext = array();
		if ($years > 0) {
			if ($years > 1) { $longtext[] = number_format($years).' years'; }
			else { $longtext[] =  '1 year'; }
		}
		if ($days > 0) {
			if ($days > 1) { $longtext[] =  number_format($days).' days'; }
			else { $longtext[] =  '1 day'; }
		}
		if ($hours > 0) {
			if ($hours > 1) { $longtext[] =  number_format($hours).' hours'; }
			else { $longtext[] =  '1 hour'; }
		}
		if ($minutes > 0) {
			if ($minutes > 1) { $longtext[] =  $minutes.' minutes'; }
			else { $longtext[] =  '1 minute'; }
		}
		if (($seconds > 0) || (count($longtext) == 0)) {
			if ($seconds != 1) { $longtext[] =  $seconds.' seconds'; }
			else { $longtext[] =  '1 second'; }
		}		
		$longtext = implode(', ',$longtext);
		if ($return == 'longtext') { return $longtext; }
		else { $hms['longtext'] = $longtext; }
	}

	//short text (1 rounded number)
	if ((!$return) || ($return == 'shorttext')) {
		if ($years > 0) {
			$shorttext = round(($total_seconds/31557600),1);
			if ($shorttext == 1) { $shorttext .= ' year'; } else { $shorttext .= ' years'; }
		} elseif ($days > 0) {
			$shorttext = round(($total_seconds/86400),1);
			if ($shorttext == 1) { $shorttext .= ' day'; } else { $shorttext .= ' days'; }
		} elseif ($hours > 0) {
			$shorttext = round(($total_seconds/3600),1);
			if ($shorttext == 1) { $shorttext .= ' hour'; } else { $shorttext .= ' hours'; }
		} elseif ($minutes > 0) {
			$shorttext = round(($total_seconds/60),1);
			if ($shorttext == 1) { $shorttext .= ' minute'; } else { $shorttext .= ' minutes'; }
		} else {
			$shorttext = round($total_seconds,1);
			if ($shorttext == 1) { $shorttext .= ' second'; } else { $shorttext .= ' seconds'; }
		}
		if ($return == 'shorttext') { return $shorttext; }
		else { $hms['shorttext'] = $shorttext; }
	}

	return $hms;
}

//shortcut functions
function seconds_to_hms($seconds) {
	return hms(array('seconds'=>$seconds,'return'=>'text'));
}

function seconds_to_longtext($seconds) {
	return hms(array('seconds'=>$seconds,'return'=>'longtext'));
}

function seconds_to_shorttext($seconds) {
	return hms(array('seconds'=>$seconds,'return'=>'shorttext'));
}

//other functions
function hms_to_seconds($hours,$minutes,$seconds) {
	$seconds = ($hours * 3600) + ($minutes * 60) + $seconds;
	return $seconds;
}

function hms_string_to_seconds($string) {
	$string = trim($string);
	$p = explode(':',$string);
	if ((count($p) == 1) && (is_numeric($p['0']))) { $h = 0; $m = 0; $s = $p['0']; }
	elseif (count($p) == 2) { $h = 0; $m = $p['0']; $s = $p['1']; }
	elseif (count($p) == 3) { $h = $p['0']; $m = $p['1']; $s = $p['2']; }
	else { return false; } //could not recognize pattern
	return hms_to_seconds($h,$m,$s);
}


?>

Comments

Loading…

This post was first published on June 30th, 2013 and last updated on February 6th, 2018 by Robert James Reese in PHP Before using any of the code or other content in this post, you must read and agree to our terms of use.