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