summaryrefslogtreecommitdiff
path: root/smartyplugins/modifier.display_duration.php
blob: aedb7fca137bebf739f4941a9674904f9c36c8ad (plain)
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<?php
namespace Bitweaver\Plugins;

use Bitweaver\KernelTools;
/**
 * @package Smarty
 * @subpackage plugins
 */

/**
 * basic function to convert a number of seconds into a human readable format
 * 
 * @param array $pDuration Duration of event in seconds
 * @access public
 * @return TRUE on success, FALSE on failure - mErrors will contain reason for failure
 */
function smarty_modifier_display_duration( $pDuration ) {
	$units = [
		'month'  => 60 * 60 * 24 * 7 * 4,
		'week'   => 60 * 60 * 24 * 7,
		'day'    => 60 * 60 * 24,
		'hour'   => 60 * 60,
		'min'    => 60,
		'sec'    => 1,
	];

	foreach( $units as $unit => $secs ) {
		$duration[$unit] = 0;
		if( $pDuration > $secs ) {
			$duration[$unit] = floor( $pDuration / $secs );
			$pDuration = $pDuration % $secs;
		}
	}

	$ret  = !empty( $duration['month'] ) ? $duration['month'].KernelTools::tra( 'month(s)' ).' ' : '';
	$ret .= !empty( $duration['week'] )  ? $duration['week'] .KernelTools::tra( 'week(s)' ).' '  : '';
	$ret .= !empty( $duration['day'] )   ? $duration['day']  .KernelTools::tra( 'day(s)' ).' '   : '';
	$ret .= str_pad( $duration['hour'], 2, 0, STR_PAD_LEFT ).':'.str_pad( $duration['min'], 2, 0, STR_PAD_LEFT ).':'.str_pad( $duration['sec'], 2, 0, STR_PAD_LEFT );
	return $ret;
}