Get the time duration from seconds


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<?php
/**
 * @param integer secs   Number of seconds to convert
 * @return integer          Time in weeks, days, hours, minutes, seconds
 *
 */
function duration($secs) {
		$vals = array(
			'w' => (int) ($secs / 86400 / 7),
			'd' => $secs / 86400 % 7,
			'h' => $secs / 3600 % 24,
			'm' => $secs / 60 % 60,
			's' => $secs % 60
		);
		
		$ret = array();
		$added = false;
		foreach ($vals as $key => $timeSC) {
			if ($timeSC > 0 || $added) {
				$added = true;
				$ret[] = $timeSC . $key;
			}
		}
		return join(' ', $ret);
	}