PluginProbe
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder / 2.0
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder v2.0
V-3.3.0 3.2.2 3.2.1 3.2.0 3.1.4 3.1.3 3.1.2 3.1.1 3.1.0 V3.0.3 V3.0.2 -3.0.1 V_3.0.0 1.1.1 1.1.8 1.2 1.3 1.4 1.4.18 1.5.2 1.9 2.0 2.10.0 2.10.1 2.10.2 All 137 releases
bit-form / includes / Core / Util / DateTimeHelper.php

DateTimeHelper.php in Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder 2.0, at includes/Core/Util/DateTimeHelper.php

290 lines 8.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace BitCode\BitForm\Core\Util;
4
5 use DateTime;
6 use DateTimeZone;
7
8 final class DateTimeHelper {
9 private $_dateFormat;
10 private $_timeFormat;
11 private $_timezone;
12 private $_currentTime;
13 private $_currentFormat;
14
15 public function __construct() {
16 $this->_dateFormat = get_option('date_format');
17 $this->_timeFormat = get_option('time_format');
18 $this->_timezone = self::wp_timezone();
19 $this->_currentTime = current_time('mysql');
20 $this->_currentFormat = 'Y-m-d H:i:s';
21 }
22
23 public function getDate($date = null, $currentFormat = null, $currentTZ = null, $expectedFormat = null, $expectedTZ = null) {
24 if (is_null($date)) {
25 $date = $this->_currentTime;
26 $currentFormat = $this->_currentFormat;
27 $currentTZ = $this->_timezone;
28 }
29
30 $currentFormat = is_null($currentFormat) ? $this->_currentFormat : $currentFormat;
31 $currentTZ = is_null($currentTZ) ? $this->_timezone : $currentTZ;
32 $expectedFormat = is_null($expectedFormat) ? $this->_dateFormat : $expectedFormat;
33 $expectedTZ = is_null($expectedTZ) ? $this->_timezone : $expectedTZ;
34
35 return $this->getFormated($date, $currentFormat, $currentTZ, $expectedFormat, $expectedTZ);
36 }
37
38 public function getTime($date = null, $currentFormat = null, $currentTZ = null, $expectedFormat = null, $expectedTZ = null) {
39 if (is_null($date)) {
40 $date = $this->_currentTime;
41 $currentFormat = $this->_currentFormat;
42 $currentTZ = $this->_timezone;
43 }
44
45 $currentFormat = is_null($currentFormat) ? $this->_currentFormat : $currentFormat;
46 $currentTZ = is_null($currentTZ) ? $this->_timezone : $currentTZ;
47 $expectedFormat = is_null($expectedFormat) ? $this->_timeFormat : $expectedFormat;
48 $expectedTZ = is_null($expectedTZ) ? $this->_timezone : $expectedTZ;
49
50 return $this->getFormated($date, $currentFormat, $currentTZ, $expectedFormat, $expectedTZ);
51 }
52
53 public function getDay($nameType, $date = null, $currentFormat = null, $currentTZ = null, $expectedTZ = null) {
54 if (is_null($date)) {
55 $date = $this->_currentTime;
56 $currentFormat = $this->_currentFormat;
57 $currentTZ = $this->_timezone;
58 }
59
60 $currentFormat = is_null($currentFormat) ? $this->_currentFormat : $currentFormat;
61 $currentTZ = is_null($currentTZ) ? $this->_timezone : $currentTZ;
62 $expectedTZ = is_null($expectedTZ) ? $this->_timezone : $expectedTZ;
63
64 switch ($nameType) {
65 case 'numeric-with-leading':
66 $expectedFormat = 'd';
67 break;
68
69 case 'numeric-without-leading':
70 $expectedFormat = 'j';
71 break;
72
73 case 'short-name':
74 $expectedFormat = 'D';
75 break;
76
77 case 'full-name':
78 $expectedFormat = 'l';
79 break;
80
81 default:
82 $expectedFormat = 'd';
83 break;
84 }
85
86 return $this->getFormated($date, $currentFormat, $currentTZ, $expectedFormat, $expectedTZ);
87 }
88
89 public function getMonth($nameType, $date = null, $currentFormat = null, $currentTZ = null, $expectedTZ = null) {
90 if (is_null($date)) {
91 $date = $this->_currentTime;
92 $currentFormat = $this->_currentFormat;
93 $currentTZ = $this->_timezone;
94 }
95
96 $currentFormat = is_null($currentFormat) ? $this->_currentFormat : $currentFormat;
97 $currentTZ = is_null($currentTZ) ? $this->_timezone : $currentTZ;
98 $expectedTZ = is_null($expectedTZ) ? $this->_timezone : $expectedTZ;
99
100 switch ($nameType) {
101 case 'numeric-with-leading':
102 $expectedFormat = 'm';
103 break;
104
105 case 'numeric-without-leading':
106 $expectedFormat = 'n';
107 break;
108
109 case 'short-name':
110 $expectedFormat = 'M';
111 break;
112
113 case 'full-name':
114 $expectedFormat = 'F';
115 break;
116
117 default:
118 $expectedFormat = 'd';
119 break;
120 }
121
122 return $this->getFormated($date, $currentFormat, $currentTZ, $expectedFormat, $expectedTZ);
123 }
124
125 public function getFormated($dateString, $currentFormat, $currentTZ, $expectedFormat, $expectedTZ) {
126 if (false === $currentFormat) {
127 $dateObject = new DateTime($dateString, $currentTZ);
128 } else {
129 $dateObject = DateTime::createFromFormat($currentFormat, $dateString, $currentTZ);
130 }
131
132 if (!is_null($expectedTZ)) {
133 $dateObject->setTimezone($expectedTZ);
134 }
135
136 if ($dateObject) {
137 return $dateObject->format($expectedFormat);
138 }
139 return false;
140 }
141
142 public function getUnicodeLikeFormat($type, $format = null) {
143 $type = strtolower($type);
144 switch ($type) {
145 case 'date':
146 $format = is_null($format) ? $this->_dateFormat : $format;
147 break;
148
149 case 'time':
150 $format = is_null($format) ? $this->_timeFormat : $format;
151 break;
152
153 case 'timestamp':
154 $format = is_null($format) ? $this->_currentFormat : $format;
155 break;
156
157 default:
158 break;
159 }
160
161 if (false !== strpos($format, 'd')) {
162 $format = str_replace('d', 'dd', $format);
163 }
164 if (false !== strpos($format, 'j')) {
165 $format = str_replace('j', 'd', $format);
166 }
167 if (false !== strpos($format, 'D')) {
168 $format = str_replace('D', 'eee', $format);
169 }
170 if (false !== strpos($format, 'I')) {
171 $format = str_replace('I', 'eeee', $format);
172 }
173 if (false !== strpos($format, 'S')) {
174 $format = str_replace('S', 'F', $format);
175 }
176 if (false !== strpos($format, 'M')) {
177 $format = str_replace('M', 'MMM', $format);
178 }
179 if (false !== strpos($format, 'F')) {
180 $format = str_replace('F', 'MMMM', $format);
181 }
182 if (false !== strpos($format, 'm')) {
183 $format = str_replace('m', 'MM', $format);
184 }
185 if (false !== strpos($format, 'n')) {
186 $format = str_replace('n', 'M', $format);
187 }
188 if (false !== strpos($format, 'y')) {
189 $format = str_replace('y', 'yy', $format);
190 }
191 if (false !== strpos($format, 'Y')) {
192 $format = str_replace('Y', 'yyyy', $format);
193 }
194 if (false !== strpos($format, 'a')) {
195 $format = str_replace('a', 'aaaa', $format);
196 }
197 if (false !== strpos($format, 'A')) {
198 $format = str_replace('A', 'aaaa', $format);
199 }
200 if (false !== strpos($format, 'g')) {
201 $format = str_replace('g', 'h', $format);
202 }
203 if (false !== strpos($format, 'G')) {
204 $format = str_replace('G', 'H', $format);
205 }
206 if (false !== strpos($format, 'h')) {
207 $format = str_replace('h', 'hh', $format);
208 }
209 if (false !== strpos($format, 'H')) {
210 $format = str_replace('H', 'HH', $format);
211 }
212 if (false !== strpos($format, 'i')) {
213 $format = str_replace('i', 'mm', $format);
214 }
215 if (false !== strpos($format, 's')) {
216 $format = str_replace('s', 'ss', $format);
217 }
218 return $format;
219 }
220
221 public function getUnicodeToPhpFormat($type, $format = null) {
222 $type = strtolower($type);
223 switch ($type) {
224 case 'date':
225 $format = is_null($format) ? $this->_dateFormat : $format;
226 break;
227
228 case 'time':
229 $format = is_null($format) ? $this->_timeFormat : $format;
230 break;
231
232 case 'timestamp':
233 $format = is_null($format) ? $this->_currentFormat : $format;
234 break;
235 default:
236 break;
237 }
238
239 if (false !== strpos($format, 'd')) {
240 $format = str_replace('dd', 'd', $format);
241 }
242 if (false !== strpos($format, 'E')) {
243 $format = str_replace('E', 'D', $format);
244 }
245 if (false !== strpos($format, 'MMMM')) {
246 $format = str_replace('MMMM', 'F', $format);
247 } elseif (false !== strpos($format, 'MMM')) {
248 $format = str_replace('MMM', 'M', $format);
249 } elseif (false !== strpos($format, 'MM')) {
250 $format = str_replace('MM', 'm', $format);
251 }
252 if (false !== strpos($format, 'yyyy')) {
253 $format = str_replace('yyyy', 'Y', $format);
254 } elseif (false !== strpos($format, 'yy')) {
255 $format = str_replace('yy', 'y', $format);
256 }
257 return $format;
258 }
259
260 public static function wp_timezone_string() {
261 if (\function_exists('wp_timezone_string')) {
262 return wp_timezone_string();
263 }
264
265 $timezone_string = get_option('timezone_string');
266
267 if ($timezone_string) {
268 return $timezone_string;
269 }
270
271 $offset = (float) get_option('gmt_offset');
272 $hours = (int) $offset;
273 $minutes = ($offset - $hours);
274
275 $sign = ($offset < 0) ? '-' : '+';
276 $abs_hour = abs($hours);
277 $abs_mins = abs($minutes * 60);
278 $tz_offset = sprintf('%s%02d:%02d', $sign, $abs_hour, $abs_mins);
279
280 return $tz_offset;
281 }
282
283 public static function wp_timezone() {
284 if (\function_exists('wp_timezone')) {
285 return wp_timezone();
286 }
287 return new DateTimeZone(self::wp_timezone_string());
288 }
289 }
290