TimeFormatterTrait::formatSeconds()   A
last analyzed

Complexity

Conditions 4
Paths 8

Size

Total Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 29
rs 9.456
c 0
b 0
f 0
cc 4
nc 8
nop 1
1
<?php
2
3
/**
4
 * Tack Tracker - A library for tracking long-running task progress
5
 *
6
 * @license http://opensource.org/licenses/MIT
7
 * @link https://github.com/caseyamcl/tasktracker
8
 * @version 2.0
9
 * @package caseyamcl/tasktracker
10
 * @author Casey McLaughlin <[email protected]>
11
 *
12
 * For the full copyright and license information, please view the LICENSE
13
 * file that was distributed with this source code.
14
 *
15
 * ------------------------------------------------------------------
16
 */
17
18
namespace TaskTracker\Helper;
19
20
/**
21
 * Time Formatter Trait
22
 *
23
 * @author Casey McLaughlin <[email protected]>
24
 */
25
trait TimeFormatterTrait
26
{
27
    /**
28
     * Format Seconds into readable walltime (HH:ii:ss)
29
     *
30
     * @param float $elapsedTime
31
     * @return string
32
     */
33
    public function formatSeconds($elapsedTime)
34
    {
35
        $seconds = floor($elapsedTime);
36
        $output = array();
37
38
        //Hours (only if $seconds > 3600)
39
        if ($seconds > 3600) {
40
            $hours    = floor($seconds / 3600);
41
            $seconds  = $seconds - (3600 * $hours);
42
            $output[] = number_format($hours, 0);
43
        }
44
45
        //Minutes
46
        if ($seconds >= 60) {
47
            $minutes  = floor($seconds / 60);
48
            $seconds  = $seconds - ($minutes * 60);
49
            $output[] = str_pad((string) $minutes, 2, '0', STR_PAD_LEFT);
50
        }
51
        else {
52
            $output[] = '00';
53
        }
54
55
        //Seconds
56
        $output[] =($seconds > 0)
57
            ? str_pad((string) $seconds, 2, '0', STR_PAD_LEFT)
58
            : '00';
59
60
        return implode(':', $output);
61
    }
62
}
63