Completed
Push — master ( ff3894...5063cc )
by Saulius
01:42
created

elapsed_time()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 13
nc 3
nop 2
dl 0
loc 22
ccs 13
cts 13
cp 1
crap 3
rs 9.2
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is part of the sauls/helpers package.
4
 *
5
 * @author    Saulius Vaičeliūnas <[email protected]>
6
 * @link      http://saulius.vaiceliunas.lt
7
 * @copyright 2018
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
13
namespace Sauls\Component\Helper;
14
15
use function define;
16
17
define('ELAPSED_TIME_LABEL_SINGLE', 'single');
18
define('ELAPSED_TIME_LABEL_PLURAL', 'plural');
19
define('DEFAULT_ELAPSED_TIME_LABELS', [
20
    ELAPSED_TIME_LABEL_SINGLE => [
21
        '{year}' => 'yr',
22
        '{month}' => 'mo',
23
        '{week}' => 'w',
24
        '{day}' => 'd',
25
        '{hour}' => 'h',
26
        '{minute}' => 'm',
27
        '{second}' => 's',
28
    ],
29
    ELAPSED_TIME_LABEL_PLURAL => [
30
        '{years}' => 'yr',
31
        '{months}' => 'mo',
32
        '{weeks}' => 'w',
33
        '{days}' => 'd',
34
        '{hours}' => 'h',
35
        '{minutes}' => 'm',
36
        '{seconds}' => 's',
37
    ],
38
]);
39
define('DEFAULT_ELAPSED_TIME_OFFSETS', [
40
    ['{year}', '{years}', 31557600],
41
    ['{month}', '{months}', 2592000],
42
    ['{week}', '{weeks}', 604800],
43
    ['{day}', '{days}', 86400],
44
    ['{hour}', '{hours}', 3600],
45
    ['{minute}', '{minutes}', 60],
46
    ['{second}', '{seconds}', 1],
47
]);
48
49
define('DAY_IN_HOURS', 24);
50
51
/**
52
 * @param string|\DateTime $date
53
 */
54
function print_elapsed_time_short($date, array $elapsedTimeLabels = []): string
55
{
56 10
    return elapsed_time($date, $elapsedTimeLabels)[0];
57
}
58
59
/**
60
 * @param string|\DateTime $date
61
 */
62
function print_elapsed_time_long($date, array $elapsedTimeLabels = []): string
63
{
64 4
    return implode(' ', elapsed_time($date, $elapsedTimeLabels));
65
}
66
67
function elapsed_time($date, array $elapsedTimeLabels = []): array
68
{
69 14
    $elapsedTimeLabels = array_merge(DEFAULT_ELAPSED_TIME_LABELS, $elapsedTimeLabels);
70
71 14
    $date = create_date($date);
72
73 14
    $time = $date->getTimestamp();
74 14
    $timeDifference = time() - $time;
75 14
    $timeLeftValues = [];
76
77 14
    foreach (DEFAULT_ELAPSED_TIME_OFFSETS as list($timeSingle, $timePlural, $offset)) {
78 14
        if ($timeDifference >= $offset) {
79 14
            $timeLeft = floor($timeDifference / $offset);
80 14
            $timeDifference -= ($timeLeft * $offset);
81 14
            $timeLeftValues[] = format_elapsed_time_string((int)$timeLeft, $elapsedTimeLabels, [
82 14
                ELAPSED_TIME_LABEL_SINGLE => $timeSingle,
83 14
                ELAPSED_TIME_LABEL_PLURAL => $timePlural,
84
            ]);
85
        }
86
    }
87
88 14
    return $timeLeftValues;
89
}
90
91
function create_date($date): \DateTime
92
{
93 14
    if (\is_string($date)) {
94 2
        $date = new \DateTime($date);
95
    }
96
97 14
    return $date;
98
}
99
100
function format_elapsed_time_string(int $timeLeft, array $elapsedTimeLabels, array $timeStrings): string
101
{
102 18
    return \sprintf(
103 18
        '%s%s', $timeLeft,
104 18
        (1 === $timeLeft
105 12
            ? strtr(
106 12
                array_get_value($timeStrings, ELAPSED_TIME_LABEL_SINGLE, ''),
107 12
                array_get_value($elapsedTimeLabels, ELAPSED_TIME_LABEL_SINGLE)
108
            )
109 16
            : strtr(
110 16
                array_get_value($timeStrings, ELAPSED_TIME_LABEL_PLURAL, ''),
111 18
                array_get_value($elapsedTimeLabels, ELAPSED_TIME_LABEL_PLURAL)
112
            )
113
        )
114
    );
115
}
116
117
/**
118
 * @param string|\DateTime $dateFrom
119
 * @param string|\DateTime $dateTo
120
 */
121
function countdown($dateFrom = 'now', $dateTo, string $outputFormat = '%s%02d:%02d:%02d'): string
122
{
123 7
    $dateFrom = $dateFrom instanceof \DateTime ? $dateFrom : new \DateTime($dateFrom);
124 7
    $dateTo = $dateTo instanceof \DateTime ? $dateTo : new \DateTime($dateTo);
125
126 7
    $dateDifference = $dateFrom->diff($dateTo);
127
128 7
    $dateFromHigherThanDateTo = $dateFrom > $dateTo;
129
130 7
    $hours = $dateDifference->h;
131 7
    $minutes = $dateDifference->i;
132 7
    $seconds = $dateDifference->s;
133
134 7
    $hours += countdown_calculate_days_in_hours($dateDifference->days);
135
136 7
    return \sprintf($outputFormat, $dateFromHigherThanDateTo ? '-' : '', $hours, $minutes, $seconds);
137
}
138
139
function countdown_calculate_days_in_hours(int $days): int
140
{
141 7
    if ($days > 0) {
142 5
        return $days * DAY_IN_HOURS;
143
    }
144
145 2
    return 0;
146
}
147