DateIntervalHelper   A
last analyzed

Complexity

Total Complexity 33

Size/Duplication

Total Lines 181
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 33
lcom 0
cbo 1
dl 0
loc 181
ccs 0
cts 86
cp 0
rs 9.76
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
C secondsToHumanTimeDuration() 0 43 11
F humanDurationFromInterval() 0 40 14
A secondsToInterval() 0 6 1
A intervalToSeconds() 0 7 1
A isValidIntervalString() 0 11 6
1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://flipboxfactory.com/software/interval/license
6
 * @link       https://www.flipboxfactory.com/software/interval/
7
 */
8
9
namespace flipbox\craft\interval\helpers;
10
11
use Craft;
12
use DateInterval;
13
use DateTimeImmutable;
14
15
/**
16
 * @author Flipbox Factory <[email protected]>
17
 * @since 1.0.0
18
 */
19
class DateIntervalHelper
20
{
21
    // Constants
22
    // =========================================================================
23
24
    /**
25
     * Number of seconds in a minute.
26
     *
27
     * @var int
28
     */
29
    const SECONDS_MINUTE = 60;
30
31
    /**
32
     * Number of seconds in an hour.
33
     *
34
     * @var int
35
     */
36
    const SECONDS_HOUR = 3600;
37
38
    /**
39
     * Number of seconds in a day.
40
     *
41
     * @var int
42
     */
43
    const SECONDS_DAY = 86400;
44
45
    /**
46
     * The number of seconds in a year.
47
     *
48
     * @var int
49
     */
50
    const SECONDS_YEAR = 31536000;
51
52
    // Public Methods
53
    // =========================================================================
54
55
    /**
56
     * @param int  $seconds     The number of seconds
57
     *
58
     * @return string
59
     */
60
    public static function secondsToHumanTimeDuration(int $seconds): string
61
    {
62
        $secondsInYear = self::SECONDS_YEAR;
63
        $secondsInDay = self::SECONDS_DAY;
64
        $secondsInHour = self::SECONDS_HOUR;
65
        $secondsInMinute = self::SECONDS_MINUTE;
66
67
        $years = floor($seconds / $secondsInYear);
68
        $seconds %= $secondsInYear;
69
70
        $days = floor($seconds / $secondsInDay);
71
        $seconds %= $secondsInDay;
72
73
        $hours = floor($seconds / $secondsInHour);
74
        $seconds %= $secondsInHour;
75
76
        $minutes = floor($seconds / $secondsInMinute);
77
        $seconds %= $secondsInMinute;
78
79
        $timeComponents = [];
80
81
        if ($years) {
82
            $timeComponents[] = $years.' '.($years == 1 ? Craft::t('app', 'year') : Craft::t('app', 'years'));
83
        }
84
85
        if ($days) {
86
            $timeComponents[] = $days.' '.($days == 1 ? Craft::t('app', 'day') : Craft::t('app', 'days'));
87
        }
88
89
        if ($hours) {
90
            $timeComponents[] = $hours.' '.($hours == 1 ? Craft::t('app', 'hour') : Craft::t('app', 'hours'));
91
        }
92
93
        if ($minutes) {
94
            $timeComponents[] = $minutes.' '.($minutes == 1 ? Craft::t('app', 'minute') : Craft::t('app', 'minutes'));
95
        }
96
97
        if ($seconds) {
98
            $timeComponents[] = $seconds.' '.($seconds == 1 ? Craft::t('app', 'second') : Craft::t('app', 'seconds'));
99
        }
100
101
        return implode(', ', $timeComponents);
102
    }
103
104
    /**
105
     * Returns the interval in a human-friendly string.
106
     *
107
     * @param DateInterval $dateInterval
108
     *
109
     * @return string
110
     */
111
    public static function humanDurationFromInterval(DateInterval $dateInterval): string
112
    {
113
        $timeComponents = [];
114
115
        if ($dateInterval->invert) {
116
            $timeComponents[] = '-';
117
        }
118
119
        if ($dateInterval->y) {
120
            $timeComponents[] = $dateInterval->y.' '.
121
                ($dateInterval->y == 1 ? Craft::t('app', 'year') : Craft::t('app', 'years'));
122
        }
123
124
        if ($dateInterval->m) {
125
            $timeComponents[] = $dateInterval->m.' '.
126
                ($dateInterval->m == 1 ? Craft::t('app', 'month') : Craft::t('app', 'months'));
127
        }
128
129
        if ($dateInterval->d) {
130
            $timeComponents[] = $dateInterval->d.' '.
131
                ($dateInterval->d == 1 ? Craft::t('app', 'day') : Craft::t('app', 'days'));
132
        }
133
134
        if ($dateInterval->h) {
135
            $timeComponents[] = $dateInterval->h.' '.
136
                ($dateInterval->h == 1 ? Craft::t('app', 'hour') : Craft::t('app', 'hours'));
137
        }
138
139
        if ($dateInterval->i) {
140
            $timeComponents[] = $dateInterval->i.' '.
141
                ($dateInterval->i == 1 ? Craft::t('app', 'minute') : Craft::t('app', 'minutes'));
142
        }
143
144
        if ($dateInterval->s) {
145
            $timeComponents[] = $dateInterval->s.' '.
146
                ($dateInterval->s == 1 ? Craft::t('app', 'second') : Craft::t('app', 'seconds'));
147
        }
148
149
        return implode(' ', $timeComponents);
150
    }
151
152
    /**
153
     * Creates a DateInterval object based on a given number of seconds.
154
     *
155
     * @param int $seconds
156
     *
157
     * @return DateInterval
158
     */
159
    public static function secondsToInterval(int $seconds): DateInterval
160
    {
161
        return DateInterval::createFromDateString(
162
            self::secondsToHumanTimeDuration($seconds)
163
        );
164
    }
165
166
    /**
167
     * Returns the number of seconds that a given DateInterval object spans.
168
     *
169
     * @param DateInterval $dateInterval
170
     * @return int
171
     * @throws \Exception
172
     */
173
    public static function intervalToSeconds(DateInterval $dateInterval): int
174
    {
175
        $reference = new DateTimeImmutable();
176
        $endTime = $reference->add($dateInterval);
177
178
        return $endTime->getTimestamp() - $reference->getTimestamp();
179
    }
180
181
    /**
182
     * Returns true if interval string is a valid interval.
183
     *
184
     * @param string $intervalString
185
     *
186
     * @return bool
187
     */
188
    public static function isValidIntervalString(string $intervalString): bool
189
    {
190
        $interval = DateInterval::createFromDateString($intervalString);
191
192
        return $interval->s != 0 ||
193
            $interval->i != 0 ||
194
            $interval->h != 0 ||
195
            $interval->d != 0 ||
196
            $interval->m != 0 ||
197
            $interval->y != 0;
198
    }
199
}
200