Completed
Push — master ( 3eb3fa...222099 )
by Saulius
02:58
created

countdown_calculate_days_in_hours()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 7
ccs 3
cts 3
cp 1
crap 2
rs 9.4285
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
/**
68
 * @param string|\DateTime $date
69
 */
70
function elapsed_time($date, array $elapsedTimeLabels = []): array
71
{
72 14
    $elapsedTimeLabels = array_merge(DEFAULT_ELAPSED_TIME_LABELS, $elapsedTimeLabels);
73
74 14
    if (\is_string($date)) {
75 2
        $date = new \DateTime($date);
76
    }
77
78 14
    $time = $date->getTimestamp();
79 14
    $diff = time() - $time;
80 14
    $timeLeft = [];
81
82 14
    foreach (DEFAULT_ELAPSED_TIME_OFFSETS as list($timeSingle, $timePlural, $offset)) {
83 14
        if ($diff >= $offset) {
84 14
            $left = floor($diff / $offset);
85 14
            $diff -= ($left * $offset);
86 14
            $timeLeft[] = format_elapsed_time_string((int)$left, $elapsedTimeLabels, [
87 14
                ELAPSED_TIME_LABEL_SINGLE => $timeSingle,
88 14
                ELAPSED_TIME_LABEL_PLURAL => $timePlural,
89
            ]);
90
        }
91
    }
92
93 14
    return $timeLeft;
94
}
95
96
function format_elapsed_time_string(int $timeLeft, array $elapsedTimeLabels, array $timeStrings): string
97
{
98 18
    return \sprintf(
99 18
        '%s%s', $timeLeft,
100 18
        (1 === $timeLeft
101 12
            ? strtr(
102 12
                array_get_value($timeStrings, ELAPSED_TIME_LABEL_SINGLE, ''),
103 12
                array_get_value($elapsedTimeLabels, ELAPSED_TIME_LABEL_SINGLE)
104
            )
105 16
            : strtr(
106 16
                array_get_value($timeStrings, ELAPSED_TIME_LABEL_PLURAL, ''),
107 18
                array_get_value($elapsedTimeLabels, ELAPSED_TIME_LABEL_PLURAL)
108
            )
109
        )
110
    );
111
}
112
113
/**
114
 * @param string|\DateTime $dateFrom
115
 * @param string|\DateTime $dateTo
116
 */
117
function countdown($dateFrom = 'now', $dateTo, string $outputFormat = '%s%02d:%02d:%02d'): string
118
{
119 7
    $dateFrom = $dateFrom instanceof \DateTime ? $dateFrom : new \DateTime($dateFrom);
120 7
    $dateTo = $dateTo instanceof \DateTime ? $dateTo : new \DateTime($dateTo);
121
122 7
    $dateDifference = $dateFrom->diff($dateTo);
123
124 7
    $dateFromHigherThanDateTo = $dateFrom > $dateTo;
125
126 7
    $hours = $dateDifference->h;
127 7
    $minutes = $dateDifference->i;
128 7
    $seconds = $dateDifference->s;
129
130 7
    $hours += countdown_calculate_days_in_hours($dateDifference->days);
131
132 7
    return \sprintf($outputFormat, $dateFromHigherThanDateTo ? '-' : '', $hours, $minutes, $seconds);
133
}
134
135
function countdown_calculate_days_in_hours(int $days): int
136
{
137 7
    if ($days > 0) {
138 5
        return $days * DAY_IN_HOURS;
139
    }
140
141 2
    return 0;
142
}
143