Completed
Push — develop ( e4a933...b1fd58 )
by Tom
03:38
created

DateTime::difference()   C

Complexity

Conditions 8
Paths 65

Size

Total Lines 24
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 24
rs 5.7377
c 0
b 0
f 0
cc 8
eloc 18
nc 65
nop 2
1
<?php
2
3
namespace N98\Util;
4
5
use DateTime as PhpDateTime;
6
7
class DateTime
8
{
9
    /**
10
     * Human-readable string with time difference
11
     *
12
     * @param PhpDateTime $time1
13
     * @param PhpDateTime $time2
14
     *
15
     * @return string
16
     */
17
    public static function difference(PhpDateTime $time1, PhpDateTime $time2)
18
    {
19
        if ($time1 == $time2) {
20
            return '0';
21
        }
22
23
        $interval = $time1->diff($time2);
24
        $years = $interval->format('%y');
25
        $months = $interval->format('%m');
26
        $days = $interval->format('%d');
27
        $hours = $interval->format('%h');
28
        $minutes = $interval->format('%i');
29
        $seconds = $interval->format('%s');
30
31
        $differenceString
32
            = ($years ? $years . 'Y ' : '')
33
            . ($months ? $months . 'M ' : '')
34
            . ($days ? $days . 'd ' : '')
35
            . ($hours ? $hours . 'h ' : '')
36
            . ($minutes ? $minutes . 'm ' : '')
37
            . ($seconds ? $seconds . 's ' : '');
38
39
        return trim($differenceString);
40
    }
41
42
    /**
43
     * Returns a readable string with time difference
44
     *
45
     * @param PhpDateTime $time1
46
     * @param PhpDateTime $time2
47
     *
48
     * @return string
49
     */
50
    public function getDifferenceAsString(PhpDateTime $time1, PhpDateTime $time2)
51
    {
52
        return self::difference($time1, $time2);
53
    }
54
}
55