Passed
Push — master ( 87c8a6...c3fa81 )
by Eliseev
01:10
created

Date::getTimezone()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace cse\helpers;
6
7
/**
8
 * Class Date
9
 *
10
 * @package cse\helpers
11
 */
12
class Date
13
{
14
    const FORMAT_SQL = 'Y-m-d';
15
    const FORMAT_DEFAULT = 'd.m.Y';
16
17
    const DEFAULT_TIMEZONE = 'UTC';
18
19
    /**
20
     * Get time by date
21
     *
22
     * @param $date
23
     *
24
     * @return int|null
25
     */
26
    public static function getTime($date = 'now'): ?int
27
    {
28
        if (is_string($date)) {
29
            $date = strtotime($date);
30
        } elseif (!is_int($date)) {
31
            return null;
32
        }
33
34
        if ($date < 0) return null;
35
36
        return (int) $date;
37
    }
38
39
    /**
40
     * Convert date to format
41
     *
42
     * @param $date
43
     * @param string $format
44
     *
45
     * @return null|string
46
     */
47
    public static function toFormat($date = 'now', string $format = self::FORMAT_DEFAULT): ?string
48
    {
49
        $date = self::getTime($date);
50
51
        return empty($date) ? null : date($format, $date);
52
    }
53
54
    /**
55
     * Convert date to SQL
56
     *
57
     * @param $date
58
     *
59
     * @return null|string
60
     */
61
    public static function toSQL($date = 'now'): ?string
62
    {
63
        $date = self::getTime($date);
64
65
        return  empty($date) ? null : date(self::FORMAT_SQL, $date);
66
    }
67
68
    /**
69
     * Diff dates
70
     *
71
     * @param $firstDate
72
     * @param $secondDate
73
     * @param string $type
74
     *
75
     * @return null|string
76
     */
77
    public static function diff($firstDate, $secondDate = 'now', string $type = '%d'): ?string
78
    {
79
        $firstDate = self::getTime($firstDate);
80
        $secondDate = self::getTime($secondDate);
81
82
        if (empty($secondDate) || empty($firstDate)) return null;
83
84
        if ($firstDate > $secondDate) {
85
            $startDate = new \DateTime(date('Y-m-d H:i:s', $firstDate));
86
            $endDate = new \DateTime(date('Y-m-d H:i:s', $secondDate));
87
        } else {
88
            $startDate = new \DateTime(date('Y-m-d H:i:s', $secondDate));
89
            $endDate = new \DateTime(date('Y-m-d H:i:s', $firstDate));
90
        }
91
92
93
        return $endDate->diff($startDate)->format($type);
94
    }
95
96
    /**
97
     * Get current date
98
     *
99
     * @param string $format
100
     *
101
     * @return string
102
     */
103
    public static function current(string $format = self::FORMAT_DEFAULT): string
104
    {
105
        return date($format);
106
    }
107
108
    /**
109
     * Get date last month by date
110
     *
111
     * @example
112
     * format fist day: 'Y-m-01'
113
     * format last day: 'Y-m-d'
114
     *
115
     * @param $date
116
     * @param string $format
117
     *
118
     * @return null|string
119
     */
120
    public static function extremeMonthDate($date = 'now', string $format = self::FORMAT_SQL): ?string
121
    {
122
        $date = self::getTime($date);
123
124
        return empty($date) ? null : date($format, strtotime(date('Y-m-01 00:00:00', $date)) - 1);
125
    }
126
127
    /**
128
     * Get quarter by date
129
     *
130
     * @param $date
131
     *
132
     * @return null|int
133
     */
134
    public static function getQuarter($date = 'now'): ?int
135
    {
136
        $date = self::getTime($date);
137
138
        return empty($date) ? null : (int) ceil(date('n', $date) / 3);
139
    }
140
141
    /**
142
     * Get quarter by month
143
     *
144
     * @param $numberMonth
145
     *
146
     * @return int
147
     */
148
    public static function getQuarterByNumberMonth($numberMonth): int
149
    {
150
        return (int) ceil((int) $numberMonth / 3);
151
    }
152
153
    /**
154
     * Set timezone
155
     *
156
     * @param string $timezone
157
     */
158
    public static function setTimezone(string $timezone = self::DEFAULT_TIMEZONE): void
159
    {
160
        date_default_timezone_set($timezone);
161
    }
162
163
    /**
164
     * Get timezone
165
     *
166
     * @return string
167
     */
168
    public static function getTimezone(): string
169
    {
170
        return date_default_timezone_get();
171
    }
172
173
    /**
174
     * Is timezone
175
     *
176
     * @param string $timezone
177
     *
178
     * @return bool
179
     */
180
    public static function isTimezone(string $timezone = self::DEFAULT_TIMEZONE): bool
181
    {
182
        return self::getTimezone() == $timezone;
183
    }
184
185
    /**
186
     * Change days
187
     *
188
     * @param $date
189
     * @param $day
190
     * @param string $format
191
     *
192
     * @return string
193
     */
194
    public static function changeDay($date, $day, string $format = self::FORMAT_DEFAULT): string
195
    {
196
        return date($format, strtotime($date . ' ' . ($day >= 0 ? '+' . $day : $day) . ' day'));
197
    }
198
199
    /**
200
     * Is today date
201
     *
202
     * @param $date
203
     *
204
     * @return bool
205
     */
206
    public static function isToday($date): bool
207
    {
208
        return self::toSQL($date) == date(self::FORMAT_SQL);
209
    }
210
211
    /**
212
     * Check date by timestemp
213
     *
214
     * @param int $timestemp
215
     *
216
     * @return bool
217
     */
218
    public static function checkDateByTimestamp(int $timestemp): bool
219
    {
220
        $arDate = explode('/', date('Y/m/d', $timestemp));
221
        return checkdate((int) $arDate[1], (int) $arDate[2], (int) $arDate[0]);
222
    }
223
}