Passed
Push — feature/manager_poster_index ( 2dedd6...b4b3c2 )
by Chris
06:27
created

humanizeDateDiff()   A

Complexity

Conditions 5
Paths 8

Size

Total Lines 29
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 5.025

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 21
dl 0
loc 29
rs 9.2728
c 1
b 0
f 0
cc 5
nc 8
nop 2
ccs 18
cts 20
cp 0.9
crap 5.025
1
<?php
2
0 ignored issues
show
Coding Style introduced by
Missing file doc comment
Loading history...
3
use Jenssegers\Date\Date;
4
use Illuminate\Support\Facades\Lang;
5
6
if (!function_exists('humanizeDateDiff')) {
7
    /**
8
     * Computes a human readable time difference between two dates.
9
     *
10
     * @param Date $datetime DateTime object to measure.
11
     * @param Date $origin   DateTime object to use as a reference.
12
     *
13
     * @return string
14
     */
15
    function humanizeDateDiff(Date $datetime, Date $origin = null) : string
16
    {
17 4
        if (!isset($origin)) {
18 4
            $origin = new Date();
19
        }
20 4
        $interval = $datetime->diff($origin);
21
22 4
        $d = $interval->d;
23 4
        $h = $interval->h;
24 4
        $m = $interval->i;
25 4
        $s = $interval->s;
26
27 4
        if ($d > 0) {
28 4
            $unit = 'day';
29 4
            $count = $d;
30 2
        } elseif ($h > 0) {
31 2
            $unit = 'hour';
32 2
            $count = $h;
33 2
        } elseif ($m > 0) {
34 2
            $unit = 'minute';
35 2
            $count = $m;
36
        } else {
37
            $unit = 'second';
38
            $count = $s;
39
        }
40
41 4
        $key = "common/time.$unit";
42
43 4
        return Lang::choice($key, $count);
44
    }
45
}
46