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

Here's a function (plus two 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.

1.   <?php
2.   
3.   //the main (new) function
4.   function hms($args=array()) {
5.   	$defaults = array(
6.   		'hours'=>0, //incoming hours
7.   		'minutes'=>0, //incoming minutes
8.   		'seconds'=>0, //incoming seconds
9.   		'precision'=>0, //outgoing precision: "minutes" for minutes (no seconds) or an integer for precision of seconds
10.  		'return'=>null, //what to return; if null, everything is returned in an array
11.  	);
12.  	foreach ($defaults as $key => $default) {
13.  		if (array_key_exists($key,$args)) { $$key = $args[$key]; } else { $$key = $default; }
14.  	}	
15.  
16.  	$hours = abs($hours);
17.  	$minutes = abs($minutes);
18.  	$seconds = abs($seconds);
19.  	$total_seconds = ($hours * 3600) + ($minutes * 60) + $seconds;
20.  	unset($hours,$minutes,$seconds); //to prevent confusion
21.  
22.  	if ($precision === 'minutes') { $total_seconds = round($total_seconds/60) * 60; }
23.  	elseif ($precision === 0) { $total_seconds = round($total_seconds); }
24.  	elseif ($precision !== false) { $total_seconds = round($total_seconds,$precision); }
25.  
26.  	//calculate years, days, hours, minutes, seconds
27.  	$remaining_seconds = $total_seconds;
28.  	$years = floor($remaining_seconds / 31557600); //365.25 days
29.  	$remaining_seconds = $remaining_seconds - ($years * 31557600);
30.  	$days = floor($remaining_seconds / 86400);
31.  	$remaining_seconds = $remaining_seconds - ($days * 86400);
32.  	$hours = floor($remaining_seconds / 3600);
33.  	$remaining_seconds = $remaining_seconds - ($hours * 3600);
34.  	$minutes = floor($remaining_seconds / 60);
35.  	$seconds = $remaining_seconds - ($minutes * 60);
36.  	unset($remaining_seconds);
37.  
38.  	//start return array (if we're returning an array)
39.  	if (!$return) {
40.  		$hms = array('total_seconds'=>$total_seconds,'years'=>$years,'days'=>$days,'hours'=>$hours,'minutes'=>$minutes,'seconds'=>$seconds);
41.  	}
42.  
43.  	//if text, and value is over 24 hours, switch to shorttext
44.  	if (($return == 'text') && ($days > 0)) { $return = 'shorttext'; }
45.  
46.  	//text (h:mm:ss)
47.  	if ((!$return) || ($return == 'text')) {
48.  		if ($hours > 0) {
49.  			$hh = $hours;
50.  			$text = $hours.':';
51.  		} else {
52.  			$hh = '';
53.  			$text = '';
54.  		}
55.  		if (($minutes >= 10) || ($hours == 0)) { $mm = $minutes; }
56.  		elseif ($hours > 0) { $mm = '0'.$minutes; }
57.  		if (($minutes > 0) || ($hours > 0)) {
58.  			$text .= $mm.':';
59.  		} else {
60.  			$mm = '';
61.  		}
62.  		if ((($minutes > 0) || ($hours > 0)) && ($seconds == 0)) { $ss = '00'; }
63.  		elseif (($seconds >= 10) || ($minutes < 1)) { $ss = $seconds; }
64.  		else { $ss = '0'.$seconds; }
65.  		$text .= $ss;
66.  
67.  		if ($return == 'text') { return $text; }
68.  		else {
69.  			$hms['text'] = $text;
70.  			$hms['hh'] = $hh; //blank (instead of "0") if no hours
71.  			$hms['mm'] = $mm; //includes preceding zero so hh:mm:ss looks right, blank if no hours and no minutes
72.  			$hms['ss'] = $ss; //includes preceding zero so hh:mm:ss looks right
73.  		}
74.  	}
75.  
76.  	//long text (comma separated numbers and words)
77.  	if ((!$return) || ($return == 'longtext')) {
78.  		$longtext = array();
79.  		if ($years > 0) {
80.  			if ($years > 1) { $longtext[] = number_format($years).' years'; }
81.  			else { $longtext[] =  '1 year'; }
82.  		}
83.  		if ($days > 0) {
84.  			if ($days > 1) { $longtext[] =  number_format($days).' days'; }
85.  			else { $longtext[] =  '1 day'; }
86.  		}
87.  		if ($hours > 0) {
88.  			if ($hours > 1) { $longtext[] =  number_format($hours).' hours'; }
89.  			else { $longtext[] =  '1 hour'; }
90.  		}
91.  		if ($minutes > 0) {
92.  			if ($minutes > 1) { $longtext[] =  $minutes.' minutes'; }
93.  			else { $longtext[] =  '1 minute'; }
94.  		}
95.  		if (($seconds > 0) || (count($longtext) == 0)) {
96.  			if ($seconds != 1) { $longtext[] =  $seconds.' seconds'; }
97.  			else { $longtext[] =  '1 second'; }
98.  		}		
99.  		$longtext = implode(', ',$longtext);
100. 		if ($return == 'longtext') { return $longtext; }
101. 		else { $hms['longtext'] = $longtext; }
102. 	}
103. 
104. 	//short text (1 rounded number)
105. 	if ((!$return) || ($return == 'shorttext')) {
106. 		if ($years > 0) {
107. 			$shorttext = round(($total_seconds/31557600),1);
108. 			if ($shorttext == 1) { $shorttext .= ' year'; } else { $shorttext .= ' years'; }
109. 		} elseif ($days > 0) {
110. 			$shorttext = round(($total_seconds/86400),1);
111. 			if ($shorttext == 1) { $shorttext .= ' day'; } else { $shorttext .= ' days'; }
112. 		} elseif ($hours > 0) {
113. 			$shorttext = round(($total_seconds/3600),1);
114. 			if ($shorttext == 1) { $shorttext .= ' hour'; } else { $shorttext .= ' hours'; }
115. 		} elseif ($minutes > 0) {
116. 			$shorttext = round(($total_seconds/60),1);
117. 			if ($shorttext == 1) { $shorttext .= ' minute'; } else { $shorttext .= ' minutes'; }
118. 		} else {
119. 			$shorttext = round($total_seconds,1);
120. 			if ($shorttext == 1) { $shorttext .= ' second'; } else { $shorttext .= ' seconds'; }
121. 		}
122. 		if ($return == 'shorttext') { return $shorttext; }
123. 		else { $hms['shorttext'] = $shorttext; }
124. 	}
125. 
126. 	return $hms;
127. }
128. 
129. //shortcut functions
130. function seconds_to_hms($seconds) {
131. 	return hms(array('seconds'=>$seconds,'return'=>'text'));
132. }
133. 
134. function seconds_to_longtext($seconds) {
135. 	return hms(array('seconds'=>$seconds,'return'=>'longtext'));
136. }
137. 
138. function seconds_to_shorttext($seconds) {
139. 	return hms(array('seconds'=>$seconds,'return'=>'shorttext'));
140. }
141. 
142. //other functions
143. function hms_to_seconds($hours,$minutes,$seconds) {
144. 	$seconds = ($hours * 3600) + ($minutes * 60) + $seconds;
145. 	return $seconds;
146. }
147. 
148. function hms_string_to_seconds($string) {
149. 	$string = trim($string);
150. 	$p = explode(':',$string);
151. 	if ((count($p) == 1) && (is_numeric($p['0']))) { $h = 0; $m = 0; $s = $p['0']; }
152. 	elseif (count($p) == 2) { $h = 0; $m = $p['0']; $s = $p['1']; }
153. 	elseif (count($p) == 3) { $h = $p['0']; $m = $p['1']; $s = $p['2']; }
154. 	else { return false; } //could not recognize pattern
155. 	return hms_to_seconds($h,$m,$s);
156. }
157. 
158. //the old function
159. //delete the commented out old function when possible... a legacy workaround is in place here:
160. function hms_conversion($hours,$minutes,$seconds,$precision=0,$include_seconds_in_longtext=true) {
161. 	return hms(array('hours'=>$hours,'minutes'=>$minutes,'seconds'=>$seconds,'precision'=>$precision));
162. }
163. /*function hms_conversion($hours,$minutes,$seconds,$precision=0,$include_seconds_in_longtext=true) {
164. 	$hours = abs($hours);
165. 	$minutes = abs($minutes);
166. 	$seconds = abs($seconds);
167. 	$total_seconds = ($hours * 3600) + ($minutes * 60) + $seconds;
168. 	if ($precision === 0) { $total_seconds = round($total_seconds); }
169. 	elseif ($precision !== false) { $total_seconds = round($total_seconds,$precision); }
170. 
171. 	//calculate years, days, hours, minutes, seconds
172. 	$remaining_seconds = $total_seconds;
173. 
174. 	$years = floor($remaining_seconds / 31557600); //365.25 days
175. 	$remaining_seconds = $remaining_seconds - ($years * 31557600);
176. 	
177. 	$days = floor($remaining_seconds / 86400);
178. 	$remaining_seconds = $remaining_seconds - ($days * 86400);
179. 
180. 	$hours = floor($remaining_seconds / 3600);
181. 	$remaining_seconds = $remaining_seconds - ($hours * 3600);
182. 
183. 	$minutes = floor($remaining_seconds / 60);
184. 
185. 	$seconds = $remaining_seconds - ($minutes * 60);
186. 
187. 	unset($remaining_seconds);
188. 
189. 	$hms = array('total_seconds'=>$total_seconds,'years'=>$years,'days'=>$days,'hours'=>$hours,'minutes'=>$minutes,'seconds'=>$seconds,'shorttext'=>null);
190. 
191. 	//now, format text and longtext
192. 	$text = '';
193. 	$longtext = '';
194. 	if ($years > 0) {
195. 		if ($years > 1) { $longtext .= number_format($years).' years, '; }
196. 		else { $longtext .= '1 year, '; }
197. 		$hms['shorttext'] = round(($total_seconds/31557600),1).'';
198. 		if ($hms['shorttext'] == 1) { $hms['shorttext'] .= ' year'; } else { $hms['shorttext'] .= ' years'; }
199. 	}
200. 	if ($days > 0) {
201. 		if ($days > 1) { $longtext .= number_format($days).' days, '; }
202. 		else { $longtext .= '1 day, '; }
203. 		if (!$hms['shorttext']) {
204. 			$hms['shorttext'] = round(($total_seconds/86400),1).'';
205. 			if ($hms['shorttext'] == 1) { $hms['shorttext'] .= ' day'; } else { $hms['shorttext'] .= ' days'; }
206. 		}
207. 	}
208. 	if ($hours > 0) {
209. 		$hh = $hours;
210. 		$text .= $hours.':';
211. 		if ($hours > 1) { $longtext .= number_format($hours).' hours, '; }
212. 		else { $longtext .= '1 hour, '; }
213. 		if (!$hms['shorttext']) {
214. 			$hms['shorttext'] = round(($total_seconds/3600),1).'';
215. 			if ($hms['shorttext'] == 1) { $hms['shorttext'] .= ' hour'; } else { $hms['shorttext'] .= ' hours'; }
216. 		}
217. 	} else {
218. 		$hh = '';
219. 	}
220. 	if (($minutes >= 10) || ($hours == 0)) { $mm = $minutes; }
221. 	elseif ($hours > 0) { $mm = '0'.$minutes; }
222. 	if (($minutes > 0) || ($hours > 0)) {
223. 		$text .= $mm.':';
224. 		if ($minutes != 1) { $longtext .= $minutes.' minutes, '; }
225. 		else { $longtext .= '1 minute, '; }
226. 	} else {
227. 		$mm = '';
228. 	}
229. 	if (($minutes > 0) && (!$hms['shorttext'])) {
230. 		$hms['shorttext'] = round(($total_seconds/60),1).'';
231. 		if ($hms['shorttext'] == 1) { $hms['shorttext'] .= ' minute'; } else { $hms['shorttext'] .= ' minutes'; }
232. 	}
233. 	if (!$hms['shorttext']) {
234. 		$hms['shorttext'] = round($total_seconds,1).'';
235. 		if ($hms['shorttext'] == 1) { $hms['shorttext'] .= ' second'; } else { $hms['shorttext'] .= ' seconds'; }
236. 	}
237. 	if ((($minutes > 0) || ($hours > 0)) && ($seconds == 0)) { $ss = '00'; }
238. 	elseif (($seconds >= 10) || ($seconds == $total_seconds)) { $ss = $seconds; }
239. 	else { $ss = '0'.$seconds; }
240. 	$text .= $ss;
241. 	if ($include_seconds_in_longtext) {
242. 		if ($seconds != 1) { $longtext .= $seconds.' seconds'; }
243. 		else { $longtext .= '1 second'; }
244. 	} else {
245. 		if ($longtext) { $longtext = substr($longtext,0,-2); } //get rid of ", "
246. 	}
247. 	$hms['hh'] = $hh; //blank (instead of "0") if no hours
248. 	$hms['mm'] = $mm; //includes preceding zero so hh:mm:ss looks right, blank if no hours and no minutes
249. 	$hms['ss'] = $ss; //includes preceding zero so hh:mm:ss looks right
250. 	if ($days > 0) { $hms['text'] = '> 24:00:00'; } //this isn't intended to be used this way
251. 	else { $hms['text'] = $text; }
252. 	$hms['longtext'] = $longtext;
253. 	
254. 	return $hms;
255. }*/
256. 
257. //the javascript dropdowns function
258. function js_hms_dropdowns($args=array()) {
259. 	$defaults = array(
260. 		'name'=>'hms',
261. 		'value'=>0,
262. 		'onchange'=>'',
263. 		'class'=>false,
264. 	);
265. 	foreach ($defaults as $key => $default) {
266. 		if (array_key_exists($key,$args)) { $$key = $args[$key]; } else { $$key = $default; }
267. 	}
268. 	//echo '<pre>'.print_r($args,true).'</pre>';
269. 
270. 	$s = '<input type="hidden" id="'.$name.'" value="'.$value.'">';
271. 
272. 	$onchange = trim('update_hms_'.$name.'(); '.$onchange);
273. 
274. 	$remaining = $value;
275. 	$hours = floor($remaining / 3600);
276. 	$remaining = $remaining - (3600 * $hours);
277. 	$minutes = floor($remaining / 60);
278. 	$remaining = $remaining - (60 * $minutes);
279. 	$seconds = round($remaining);
280. 
281. 	if ($class) { $class = ' class="'.$class.'"'; } else { $class = ''; }
282. 
283. 	//hours
284. 	$s .= '<select id="'.$name.'_hours"'.$class.' onchange="'.$onchange.'">';
285. 	for ($i=0;$i<=48;$i++) {
286. 		if ($i == $hours) { $selected = ' selected'; } else { $selected = ''; }
287. 		$s .= '<option value="'.$i.'"'.$selected.'>'.$i.'</option> ';
288. 	}
289. 	$s .= '</select> ';
290. 
291. 	//minutes
292. 	$s .= '<select id="'.$name.'_minutes"'.$class.' onchange="'.$onchange.'">';
293. 	for ($i=0;$i<60;$i++) {
294. 		if ($i < 10) { $ii = '0'.$i; } else { $ii = $i; }
295. 		if ($i == $minutes) { $selected = ' selected'; } else { $selected = ''; }
296. 		$s .= '<option value="'.$i.'"'.$selected.'>'.$ii.'</option> ';
297. 	}
298. 	$s .= '</select> ';
299. 
300. 	//seconds
301. 	$s .= '<select id="'.$name.'_seconds"'.$class.' onchange="'.$onchange.'">';
302. 	for ($i=0;$i<60;$i++) {
303. 		if ($i < 10) { $ii = '0'.$i; } else { $ii = $i; }
304. 		if ($i == $seconds) { $selected = ' selected'; } else { $selected = ''; }
305. 		$s .= '<option value="'.$i.'"'.$selected.'>'.$ii.'</option> ';
306. 	}
307. 	$s .= '</select>';
308. 
309. 	$s .= '<script>';
310. 	$s .= 'function update_hms_'.$name.'() {';
311. 		$s .= 'var seconds = (Math.round(document.getElementById(\''.$name.'_hours\').value) * 3600)';
312. 		$s .= ' + (Math.round(document.getElementById(\''.$name.'_minutes\').value) * 60)';
313. 		$s .= ' + Math.round(document.getElementById(\''.$name.'_seconds\').value);';
314. 		$s .= 'document.getElementById(\''.$name.'\').value = seconds;';
315. 	$s .= '}';
316. 	$s .= '</script>';
317. 
318. 	return $s;
319. }
320. 
321. ?>

So you can see how it works, I've included a few examples below:

1.   $hms = hms_conversion(0,0,5400.5,0);
2.   echo $hms['longtext']; //returns 1 hour, 30 minutes, 1 second
3.   
4.   $hms = hms_conversion(0,0,5400.5,0);
5.   echo $hms['text']; //returns 1:30:01
6.   
7.   $hms = hms_conversion(2,0,5400.5,false);
8.   echo $hms['text']; //returns 3:30:00.5
9.   
10.  $hms = hms_conversion(2.5,0,0,1);
11.  echo $hms['text']; //returns 2:30:00
12.  
13.  $hms = hms_conversion(2.5,0,0,0);
14.  echo $hms['total_seconds']; //returns 9000
15.  
16.  $hms = hms_conversion(0,20.43,0,2);
17.  echo $hms['longtext']; //returns 20 minutes, 25.8 seconds
18.  
19.  $hms = hms_conversion(2,3,7,0);
20.  echo $hms['text']; //returns 2:03:07
21.  
22.  $hms = hms_conversion(2,3,7,0);
23.  echo $hms['hh']; //returns 2
24.  
25.  $hms = hms_conversion(2,3,7,0);
26.  echo $hms['mm']; //returns 03
27.  
28.  $hms = hms_conversion(2,3,7,0);
29.  echo $hms['ss']; //returns 07
30.  
31.  $hms = hms_conversion(2,3,7,0);
32.  echo $hms['minutes']; //returns 3

You can also see a good working example of this on the pace charts that we published over on our running log.


Comments

Loading…

This post was published on June 30th, 2013 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.