This repository was archived by the owner on Feb 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathphpDateManip.class.php
More file actions
346 lines (300 loc) · 9.34 KB
/
phpDateManip.class.php
File metadata and controls
346 lines (300 loc) · 9.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
<?php
/**
* Class phpDateManip
*
* Date manipulation using php DateTime. International dates supported.
* https://github.com/pontikis/phpDateManip
*
* @author Christos Pontikis http://pontikis.net
* @copyright Christos Pontikis
* @license MIT http://opensource.org/licenses/MIT
* @version 0.3.0 (XX XXX 2017)
*
*/
class phpDateManip {
private $dt;
private $a_valid_date_ranges;
private $datetime_range_start;
private $datetime_range_end;
private $datetime_string_range_start;
private $datetime_string_range_end;
private $a_valid_date_modifications;
private $a_valid_date_modification_units;
private $datetime_modified;
private $datetime_string_modified;
private $strings;
private $last_error;
public function __construct() {
$this->a_valid_date_ranges = array(
'current_year',
'current_month',
'current_week_from_monday',
'current_week_from_sunday',
'current_day'
);
$this->datetime_range_start = null;
$this->datetime_range_end = null;
$this->datetime_string_range_start = null;
$this->datetime_string_range_end = null;
$this->a_valid_date_modifications = array(
'increase',
'decrease',
);
$this->a_valid_date_modification_units = array(
'Year',
'Month',
'Week',
'Day',
'Hour',
'Min',
'Sec'
);
$this->strings = array(
'invalid_date_range' => 'Invalid date range',
'invalid_timezone' => 'Invalid timezone',
'invalid_date_modification' => 'Invalid date modification',
'invalid_date_modification_quantity' => 'Invalid quantity for date modification',
'invalid_date_modification_unit' => 'Invalid unit for date modification',
'invalid_date_to_modify' => 'Invalid date to modify'
);
$this->datetime_modified = null;
$this->datetime_string_modified = null;
$this->last_error = null;
}
// getters -----------------------------------------------------------------
public function getError() {
return $this->last_error;
}
public function getDatetimeRangeStart() {
return $this->datetime_range_start;
}
public function getDatetimeRangeEnd() {
return $this->datetime_range_end;
}
public function getDatetimeStringRangeStart() {
return $this->datetime_string_range_start;
}
public function getDatetimeStringRangeEnd() {
return $this->datetime_string_range_end;
}
public function getDatetimeModified() {
return $this->datetime_modified;
}
public function getDatetimeStringModified() {
return $this->datetime_string_modified;
}
// setters -----------------------------------------------------------------
/**
* @param array $strings
*/
public function setStrings($strings) {
$this->strings = $strings;
}
// methods -----------------------------------------------------------------
/**
* Creates date ranges
*
* Example: $res = createDateRange('current_year', 'Europe/Athens', 'j/n/Y H:i');
*
* @param $str_date_range
* @param $str_timezone
* @param $str_dateformat_not_intl
* @param array $a_intl_settings
* @return bool
*/
public function createDateRange($str_date_range, $str_timezone, $str_dateformat_not_intl, array $a_intl_settings = array()) {
if(!in_array($str_date_range, $this->a_valid_date_ranges)) {
$this->last_error = $this->strings['invalid_date_range'];
return false;
}
if(!$this->isValidTimezone($str_timezone)) {
$this->last_error = $this->strings['invalid_timezone'];
return false;
}
$d = new DateTime('now', new DateTimeZone($str_timezone));
switch($str_date_range) {
case 'current_year':
$d->modify('first day of january');
break;
case 'current_month':
$d->modify('first day of this month');
break;
case 'current_week_from_monday':
$d->modify('monday this week');
break;
case 'current_week_from_sunday':
if($d->format('D') != 'Sun') {
$d->modify('previous sunday');
}
break;
case 'current_day':
break;
}
$d->setTime(0, 0, 0);
$this->datetime_range_start = $d;
$this->datetime_string_range_start = $this->formatDateTime($d, $str_dateformat_not_intl, $a_intl_settings);
switch($str_date_range) {
case 'current_year':
$d->add(DateInterval::createFromDateString('+1 Year'));
break;
case 'current_month':
$d->add(DateInterval::createFromDateString('+1 Month'));
break;
case 'current_week_from_monday':
$d->add(DateInterval::createFromDateString('+1 Week'));
break;
case 'current_week_from_sunday':
$d->add(DateInterval::createFromDateString('+1 Week'));
break;
case 'current_day':
$d->add(DateInterval::createFromDateString('+1 Day'));
break;
}
$this->datetime_range_end = $d;
$this->datetime_string_range_end = $this->formatDateTime($d, $str_dateformat_not_intl, $a_intl_settings);
return true;
}
/**
* Modifies (increase or decrease) a datetime string
*
* Example: $res = $phpDateManip->modifyDateString('increase', 3, 'Month', '1/1/2017 00:00', 'Europe/Athens', 'j/n/Y H:i');
*
* @param string $str_action Valid options: "increase decrease"
* @param int $int_quantity
* @param string $str_unit Valid options: "Year Month Week Day Hour Min Sec"
* @param string $str_date
* @param string $str_timezone
* @param string $str_dateformat_not_intl
* @param array $a_intl_settings
* @return bool
*/
public function modifyDateString($str_action,
$int_quantity,
$str_unit,
$str_date,
$str_timezone,
$str_dateformat_not_intl,
array $a_intl_settings = array()) {
if(!in_array($str_action, $this->a_valid_date_modifications)) {
$this->last_error = $this->strings['invalid_date_modification'];
return false;
}
if(!$this->_is_positive_integer($int_quantity, 3)) {
$this->last_error = $this->strings['invalid_date_modification_quantity'];
return false;
}
if(!in_array($str_unit, $this->a_valid_date_modification_units)) {
$this->last_error = $this->strings['invalid_date_modification_unit'];
return false;
}
if(!$this->isValidTimezone($str_timezone)) {
$this->last_error = $this->strings['invalid_timezone'];
return false;
}
$resDate = $this->isValidDateTimeString($str_date, $str_dateformat_not_intl, $str_timezone, $a_intl_settings);
if(!$resDate) {
$this->last_error = $this->strings['invalid_date_to_modify'];
return false;
} else {
if($a_intl_settings) {
$d = new DateTime('now', new DateTimeZone($str_timezone));
$d->setTimestamp($resDate); // integer timestamp
} else {
$d = DateTime::createFromFormat($str_dateformat_not_intl, $str_date, new DateTimeZone($str_timezone));
}
$str_interval = ($str_action == 'increase' ? '+' : '-');
$str_interval .= $int_quantity;
$str_interval .= ' ';
$str_interval .= $str_unit;
$d->add(DateInterval::createFromDateString($str_interval));
$this->datetime_modified = $d;
$this->datetime_string_modified = $this->formatDateTime($d, $str_dateformat_not_intl, $a_intl_settings);
return true;
}
}
/**
* Format datetime object
*
* Example: $res = $phpDateManip->formatDateTime($d, 'j/n/Y H:i');
*
* @param DateTime $d
* @param $str_dateformat_not_intl
* @param array $a_intl_settings
* @return string
*/
public function formatDateTime(DateTime $d, $str_dateformat_not_intl, array $a_intl_settings = array()) {
if($a_intl_settings) {
$formatter = new IntlDateFormatter(
$a_intl_settings['locale'],
$a_intl_settings['datetype'],
$a_intl_settings['timetype'],
$a_intl_settings['timezone'],
$a_intl_settings['calendar'],
$a_intl_settings['pattern']
);
return $formatter->format($d);
} else {
return $d->format($str_dateformat_not_intl);
}
}
// utility functions -------------------------------------------------------
/**
* Check if a string is a valid timezone
*
* timezone_identifiers_list() requires PHP >= 5.2
*
* @param string $timezone
* @return bool
*/
public function isValidTimezone($timezone) {
return in_array($timezone, timezone_identifiers_list());
}
/**
* Check if a string (of any locale) is a valid date(time)
*
* DateTime::createFromFormat requires PHP >= 5.3
*
* @param string $str_dt
* @param string $str_dateformat
* @param string $str_timezone (If timezone is invalid, php will throw an exception)
* @param array $intl international options
* @return bool|int
*/
public function isValidDateTimeString($str_dt, $str_dateformat, $str_timezone = null, $intl = array()) {
if($intl) {
$formatter = new IntlDateFormatter($intl['locale'], $intl['datetype'], $intl['timetype'], $intl['timezone'], $intl['calendar'], $intl['pattern']);
return $formatter->parse($str_dt);
} else {
if($str_timezone) {
$date = DateTime::createFromFormat($str_dateformat, $str_dt, new DateTimeZone($str_timezone));
} else {
$date = DateTime::createFromFormat($str_dateformat, $str_dt);
}
$a_err = DateTime::getLastErrors(); // compatibility with php 5.3
return $date && $a_err['warning_count'] == 0 && $a_err['error_count'] == 0;
}
}
// private functions -------------------------------------------------------
/**
* Check if expression is positive integer
*
* @param $str
* @param $length
* @return bool
*/
private function _is_positive_integer($str, $length) {
// allow only digits
if(preg_match("/[^\pN]/u", $str)) {
return false;
}
// allow only positive values
if($str == 0) {
return false;
}
// check integer length
if(strlen($str) > $length) {
return false;
}
return true;
}
}