Passed
Push — task/update-npm ( 00bdcc...33d0cb )
by Tristan
28:58 queued 13:36
created

humanizeDateDiff()   A

Complexity

Conditions 5
Paths 8

Size

Total Lines 29
Code Lines 21

Duplication

Lines 0
Ratio 0 %

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
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\App;
5
use Illuminate\Support\Facades\Config;
6
use Illuminate\Support\Facades\Lang;
7
8
if (!function_exists('humanizeDate')) {
9
    /**
10
     * Computes a human readable localized date.
11
     *
12
     * @param Date $datetime DateTime object to translate.
13
     *
14
     * @return string
15
     */
16
    function humanizeDate(Date $datetime) : string
17
    {
18
        $dateFormat = Config::get('app.date_format');
19
        $locale = App::getLocale();
20
        $timezone = Config::get('app.local_timezone');
21
22
        $datetime->setTimezone(new \DateTimeZone($timezone));
23
24
        return $datetime->format($dateFormat[$locale]);
25
    }
26
}
27
28
if (!function_exists('humanizeTime')) {
29
    /**
30
     * Computes a human readable localized time.
31
     *
32
     * @param Date $datetime DateTime object to translate.
33
     *
34
     * @return string
35
     */
36
    function humanizeTime(Date $datetime) : string
37
    {
38
        $timeFormat = Config::get('app.time_format');
39
        $locale = App::getLocale();
40
        $timezone = Config::get('app.local_timezone');
41
42
        $datetime->setTimezone(new \DateTimeZone($timezone));
43
44
        $displayTime = $datetime->format($timeFormat[$locale]);
45
46
        if ($locale == 'fr') {
47
            $displayTime = str_replace(['EST', 'EDT'], ['HNE', 'HAE'], $displayTime);
48
        }
49
50
        return $displayTime;
51
    }
52
}
53
54
if (!function_exists('humanizeDateDiff')) {
55
    /**
56
     * Computes a human readable time difference between two dates.
57
     *
58
     * @param Date $datetime DateTime object to measure.
59
     * @param Date $origin   DateTime object to use as a reference.
60
     *
61
     * @return string
62
     */
63
    function humanizeDateDiff(Date $datetime, Date $origin = null) : string
64
    {
65
        if (!isset($origin)) {
66
            $origin = new Date();
67
        }
68
        $interval = $datetime->diff($origin);
69
70
        $d = $interval->d;
71
        $h = $interval->h;
72
        $m = $interval->i;
73
        $s = $interval->s;
74
75
        if ($d > 0) {
76
            $unit = 'day';
77
            $count = $d;
78
        } elseif ($h > 0) {
79
            $unit = 'hour';
80
            $count = $h;
81
        } elseif ($m > 0) {
82
            $unit = 'minute';
83
            $count = $m;
84
        } else {
85
            $unit = 'second';
86
            $count = $s;
87
        }
88
89
        $key = "common/time.$unit";
90
91
        return Lang::choice($key, $count);
92
    }
93
}
94