PluginProbe
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder / 1.1.1
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder v1.1.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 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 1.1.1, at includes/Core/Util/DateTimeHelper.php

268 lines 8.8 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
7 final class DateTimeHelper
8 {
9 private $_dateFormat;
10 private $_timeFormat;
11 private $_timezone;
12 private $_currentTime;
13 private $_currentFormat;
14
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';
22 }
23
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 }
31
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;
36
37 return $this->getFormated($date, $currentFormat, $currentTZ, $expectedFormat, $expectedTZ);
38 }
39
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 }
47
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;
52
53 return $this->getFormated($date, $currentFormat, $currentTZ, $expectedFormat, $expectedTZ);
54 }
55
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 }
63
64 $currentFormat = is_null($currentFormat) ? $this->_currentFormat : $currentFormat;
65 $currentTZ = is_null($currentTZ) ? $this->_timezone : $currentTZ;
66 $expectedTZ = is_null($expectedTZ) ? $this->_timezone : $expectedTZ;
67
68 switch ($nameType) {
69 case 'numeric-with-leading':
70 $expectedFormat = 'd';
71 break;
72
73 case 'numeric-without-leading':
74 $expectedFormat = 'j';
75 break;
76
77 case 'short-name':
78 $expectedFormat = 'D';
79 break;
80
81 case 'full-name':
82 $expectedFormat = 'l';
83 break;
84
85 default:
86 $expectedFormat = 'd';
87 break;
88 }
89
90 return $this->getFormated($date, $currentFormat, $currentTZ, $expectedFormat, $expectedTZ);
91 }
92
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 }
100
101 $currentFormat = is_null($currentFormat) ? $this->_currentFormat : $currentFormat;
102 $currentTZ = is_null($currentTZ) ? $this->_timezone : $currentTZ;
103 $expectedTZ = is_null($expectedTZ) ? $this->_timezone : $expectedTZ;
104
105 switch ($nameType) {
106 case 'numeric-with-leading':
107 $expectedFormat = 'm';
108 break;
109
110 case 'numeric-without-leading':
111 $expectedFormat = 'n';
112 break;
113
114 case 'short-name':
115 $expectedFormat = 'M';
116 break;
117
118 case 'full-name':
119 $expectedFormat = 'F';
120 break;
121
122 default:
123 $expectedFormat = 'd';
124 break;
125 }
126
127 return $this->getFormated($date, $currentFormat, $currentTZ, $expectedFormat, $expectedTZ);
128 }
129
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 }
137
138 if (!is_null($expectedTZ)) {
139 $dateObject->setTimezone($expectedTZ);
140 }
141
142 if ($dateObject) {
143 return $dateObject->format($expectedFormat);
144 }
145 return false;
146 }
147
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;
155
156 case 'time':
157 $format = is_null($format) ? $this->_timeFormat : $format;
158 break;
159
160 case 'timestamp':
161 $format = is_null($format) ? $this->_currentFormat : $format;
162 break;
163
164 default:
165 break;
166 }
167
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;
226 }
227
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;
235
236 case 'time':
237 $format = is_null($format) ? $this->_timeFormat : $format;
238 break;
239
240 case 'timestamp':
241 $format = is_null($format) ? $this->_currentFormat : $format;
242 break;
243 default:
244 break;
245 }
246
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;
266 }
267 }
268