Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
bc77d7c0 TO |
3 | +--------------------------------------------------------------------+ |
4 | | Copyright CiviCRM LLC. All rights reserved. | | |
5 | | | | |
6 | | This work is published under the GNU AGPLv3 license with some | | |
7 | | permitted exceptions and without any warranty. For full license | | |
8 | | and copyright information, see https://civicrm.org/licensing | | |
9 | +--------------------------------------------------------------------+ | |
e70a7fc0 | 10 | */ |
6a488035 TO |
11 | |
12 | /** | |
13 | * | |
14 | * @package CRM | |
ca5cec67 | 15 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
6a488035 TO |
16 | */ |
17 | ||
18 | /** | |
19 | * Date utilties | |
20 | */ | |
21 | class CRM_Utils_Date { | |
22 | ||
23 | /** | |
100fef9d | 24 | * Format a date by padding it with leading '0'. |
6a488035 | 25 | * |
77855840 TO |
26 | * @param array $date |
27 | * ('Y', 'M', 'd'). | |
28 | * @param string $separator | |
f0fed404 | 29 | * The separator to use when formatting the date. |
f4aaa82a | 30 | * @param int|string $invalidDate what to return if the date is invalid |
6a488035 | 31 | * |
a6c01b45 CW |
32 | * @return string |
33 | * formatted string for date | |
6a488035 | 34 | */ |
00be9182 | 35 | public static function format($date, $separator = '', $invalidDate = 0) { |
6a488035 TO |
36 | if (is_numeric($date) && |
37 | ((strlen($date) == 8) || (strlen($date) == 14)) | |
38 | ) { | |
39 | return $date; | |
40 | } | |
41 | ||
42 | if (!is_array($date) || | |
43 | CRM_Utils_System::isNull($date) || | |
44 | empty($date['Y']) | |
45 | ) { | |
46 | return $invalidDate; | |
47 | } | |
48 | ||
49 | $date['Y'] = (int ) $date['Y']; | |
50 | if ($date['Y'] < 1000 || $date['Y'] > 2999) { | |
51 | return $invalidDate; | |
52 | } | |
53 | ||
54 | if (array_key_exists('m', $date)) { | |
55 | $date['M'] = $date['m']; | |
56 | } | |
57 | elseif (array_key_exists('F', $date)) { | |
58 | $date['M'] = $date['F']; | |
59 | } | |
60 | ||
a7488080 | 61 | if (!empty($date['M'])) { |
6a488035 TO |
62 | $date['M'] = (int ) $date['M']; |
63 | if ($date['M'] < 1 || $date['M'] > 12) { | |
64 | return $invalidDate; | |
65 | } | |
66 | } | |
67 | else { | |
68 | $date['M'] = 1; | |
69 | } | |
70 | ||
a7488080 | 71 | if (!empty($date['d'])) { |
6a488035 TO |
72 | $date['d'] = (int ) $date['d']; |
73 | } | |
74 | else { | |
75 | $date['d'] = 1; | |
76 | } | |
77 | ||
78 | if (!checkdate($date['M'], $date['d'], $date['Y'])) { | |
79 | return $invalidDate; | |
80 | } | |
81 | ||
82 | $date['M'] = sprintf('%02d', $date['M']); | |
83 | $date['d'] = sprintf('%02d', $date['d']); | |
84 | ||
85 | $time = ''; | |
86 | if (CRM_Utils_Array::value('H', $date) != NULL || | |
87 | CRM_Utils_Array::value('h', $date) != NULL || | |
88 | CRM_Utils_Array::value('i', $date) != NULL || | |
89 | CRM_Utils_Array::value('s', $date) != NULL | |
90 | ) { | |
91 | // we have time too.. | |
a7488080 | 92 | if (!empty($date['h'])) { |
6a488035 TO |
93 | if (CRM_Utils_Array::value('A', $date) == 'PM' or CRM_Utils_Array::value('a', $date) == 'pm') { |
94 | if ($date['h'] != 12) { | |
95 | $date['h'] = $date['h'] + 12; | |
96 | } | |
97 | } | |
98 | if ((CRM_Utils_Array::value('A', $date) == 'AM' or CRM_Utils_Array::value('a', $date) == 'am') && | |
99 | CRM_Utils_Array::value('h', $date) == 12 | |
100 | ) { | |
101 | $date['h'] = '00'; | |
102 | } | |
103 | ||
104 | $date['h'] = (int ) $date['h']; | |
105 | } | |
106 | else { | |
107 | $date['h'] = 0; | |
108 | } | |
109 | ||
110 | // in 24-hour format the hour is under the 'H' key | |
a7488080 | 111 | if (!empty($date['H'])) { |
6a488035 TO |
112 | $date['H'] = (int) $date['H']; |
113 | } | |
114 | else { | |
115 | $date['H'] = 0; | |
116 | } | |
117 | ||
a7488080 | 118 | if (!empty($date['i'])) { |
6a488035 TO |
119 | $date['i'] = (int ) $date['i']; |
120 | } | |
121 | else { | |
122 | $date['i'] = 0; | |
123 | } | |
124 | ||
125 | if ($date['h'] == 0 && $date['H'] != 0) { | |
126 | $date['h'] = $date['H']; | |
127 | } | |
128 | ||
a7488080 | 129 | if (!empty($date['s'])) { |
6a488035 TO |
130 | $date['s'] = (int ) $date['s']; |
131 | } | |
132 | else { | |
133 | $date['s'] = 0; | |
134 | } | |
135 | ||
136 | $date['h'] = sprintf('%02d', $date['h']); | |
137 | $date['i'] = sprintf('%02d', $date['i']); | |
138 | $date['s'] = sprintf('%02d', $date['s']); | |
139 | ||
140 | if ($separator) { | |
141 | $time = ' '; | |
142 | } | |
143 | $time .= $date['h'] . $separator . $date['i'] . $separator . $date['s']; | |
144 | } | |
145 | ||
146 | return $date['Y'] . $separator . $date['M'] . $separator . $date['d'] . $time; | |
147 | } | |
148 | ||
149 | /** | |
fe482240 | 150 | * Return abbreviated weekday names according to the locale. |
6a488035 | 151 | * |
1ef73b88 CW |
152 | * Array will be in localized order according to 'weekBegins' setting, |
153 | * but array keys will always match to: | |
154 | * 0 => Sun | |
155 | * 1 => Mon | |
156 | * etc. | |
157 | * | |
a6c01b45 CW |
158 | * @return array |
159 | * 0-based array with abbreviated weekday names | |
6a488035 | 160 | * |
6a488035 | 161 | */ |
1ef73b88 | 162 | public static function getAbbrWeekdayNames() { |
be2fb01f | 163 | static $days = []; |
1ef73b88 CW |
164 | if (!$days) { |
165 | // First day of the week | |
aaffa79f | 166 | $firstDay = Civi::settings()->get('weekBegins'); |
6a488035 TO |
167 | |
168 | // set LC_TIME and build the arrays from locale-provided names | |
169 | // June 1st, 1970 was a Monday | |
170 | CRM_Core_I18n::setLcTime(); | |
41ff54d3 | 171 | for ($i = $firstDay; count($days) < 7; $i = $i > 5 ? 0 : $i + 1) { |
1ef73b88 | 172 | $days[$i] = strftime('%a', mktime(0, 0, 0, 6, $i, 1970)); |
6a488035 TO |
173 | } |
174 | } | |
1ef73b88 | 175 | return $days; |
6a488035 TO |
176 | } |
177 | ||
178 | /** | |
fe482240 | 179 | * Return full weekday names according to the locale. |
6a488035 | 180 | * |
1ef73b88 CW |
181 | * Array will be in localized order according to 'weekBegins' setting, |
182 | * but array keys will always match to: | |
183 | * 0 => Sunday | |
184 | * 1 => Monday | |
185 | * etc. | |
186 | * | |
a6c01b45 CW |
187 | * @return array |
188 | * 0-based array with full weekday names | |
6a488035 | 189 | * |
6a488035 | 190 | */ |
1ef73b88 | 191 | public static function getFullWeekdayNames() { |
be2fb01f | 192 | static $days = []; |
1ef73b88 CW |
193 | if (!$days) { |
194 | // First day of the week | |
aaffa79f | 195 | $firstDay = Civi::settings()->get('weekBegins'); |
6a488035 TO |
196 | |
197 | // set LC_TIME and build the arrays from locale-provided names | |
198 | // June 1st, 1970 was a Monday | |
199 | CRM_Core_I18n::setLcTime(); | |
41ff54d3 | 200 | for ($i = $firstDay; count($days) < 7; $i = $i > 5 ? 0 : $i + 1) { |
1ef73b88 | 201 | $days[$i] = strftime('%A', mktime(0, 0, 0, 6, $i, 1970)); |
6a488035 TO |
202 | } |
203 | } | |
1ef73b88 | 204 | return $days; |
6a488035 TO |
205 | } |
206 | ||
207 | /** | |
fe482240 | 208 | * Return abbreviated month names according to the locale. |
6a488035 | 209 | * |
f4aaa82a EM |
210 | * @param bool $month |
211 | * | |
a6c01b45 CW |
212 | * @return array |
213 | * 1-based array with abbreviated month names | |
6a488035 | 214 | * |
6a488035 | 215 | */ |
00be9182 | 216 | public static function &getAbbrMonthNames($month = FALSE) { |
6a488035 TO |
217 | static $abbrMonthNames; |
218 | if (!isset($abbrMonthNames)) { | |
219 | ||
220 | // set LC_TIME and build the arrays from locale-provided names | |
221 | CRM_Core_I18n::setLcTime(); | |
222 | for ($i = 1; $i <= 12; $i++) { | |
223 | $abbrMonthNames[$i] = strftime('%b', mktime(0, 0, 0, $i, 10, 1970)); | |
224 | } | |
225 | } | |
226 | if ($month) { | |
227 | return $abbrMonthNames[$month]; | |
228 | } | |
229 | return $abbrMonthNames; | |
230 | } | |
231 | ||
232 | /** | |
fe482240 | 233 | * Return full month names according to the locale. |
6a488035 | 234 | * |
a6c01b45 CW |
235 | * @return array |
236 | * 1-based array with full month names | |
6a488035 | 237 | * |
6a488035 | 238 | */ |
00be9182 | 239 | public static function &getFullMonthNames() { |
6a488035 TO |
240 | static $fullMonthNames; |
241 | if (!isset($fullMonthNames)) { | |
242 | ||
243 | // set LC_TIME and build the arrays from locale-provided names | |
244 | CRM_Core_I18n::setLcTime(); | |
245 | for ($i = 1; $i <= 12; $i++) { | |
246 | $fullMonthNames[$i] = strftime('%B', mktime(0, 0, 0, $i, 10, 1970)); | |
247 | } | |
248 | } | |
249 | return $fullMonthNames; | |
250 | } | |
251 | ||
5bc392e6 EM |
252 | /** |
253 | * @param $string | |
254 | * | |
255 | * @return int | |
256 | */ | |
00be9182 | 257 | public static function unixTime($string) { |
6a488035 TO |
258 | if (empty($string)) { |
259 | return 0; | |
260 | } | |
261 | $parsedDate = date_parse($string); | |
262 | return mktime(CRM_Utils_Array::value('hour', $parsedDate), | |
263 | CRM_Utils_Array::value('minute', $parsedDate), | |
264 | 59, | |
265 | CRM_Utils_Array::value('month', $parsedDate), | |
266 | CRM_Utils_Array::value('day', $parsedDate), | |
267 | CRM_Utils_Array::value('year', $parsedDate) | |
268 | ); | |
269 | } | |
270 | ||
271 | /** | |
fe482240 | 272 | * Create a date and time string in a provided format. |
6a488035 TO |
273 | * |
274 | * %b - abbreviated month name ('Jan'..'Dec') | |
275 | * %B - full month name ('January'..'December') | |
276 | * %d - day of the month as a decimal number, 0-padded ('01'..'31') | |
277 | * %e - day of the month as a decimal number, blank-padded (' 1'..'31') | |
278 | * %E - day of the month as a decimal number ('1'..'31') | |
279 | * %f - English ordinal suffix for the day of the month ('st', 'nd', 'rd', 'th') | |
280 | * %H - hour in 24-hour format, 0-padded ('00'..'23') | |
281 | * %I - hour in 12-hour format, 0-padded ('01'..'12') | |
282 | * %k - hour in 24-hour format, blank-padded (' 0'..'23') | |
283 | * %l - hour in 12-hour format, blank-padded (' 1'..'12') | |
284 | * %m - month as a decimal number, 0-padded ('01'..'12') | |
285 | * %M - minute, 0-padded ('00'..'60') | |
286 | * %p - lowercase ante/post meridiem ('am', 'pm') | |
287 | * %P - uppercase ante/post meridiem ('AM', 'PM') | |
288 | * %Y - year as a decimal number including the century ('2005') | |
289 | * | |
77855840 TO |
290 | * @param string $dateString |
291 | * Date and time in 'YYYY-MM-DD hh:mm:ss' format. | |
292 | * @param string $format | |
293 | * The output format. | |
294 | * @param array $dateParts | |
295 | * An array with the desired date parts. | |
6a488035 | 296 | * |
a6c01b45 CW |
297 | * @return string |
298 | * the $format-formatted $date | |
6a488035 | 299 | */ |
00be9182 | 300 | public static function customFormat($dateString, $format = NULL, $dateParts = NULL) { |
6a488035 TO |
301 | // 1-based (January) month names arrays |
302 | $abbrMonths = self::getAbbrMonthNames(); | |
303 | $fullMonths = self::getFullMonthNames(); | |
304 | ||
305 | if (!$format) { | |
306 | $config = CRM_Core_Config::singleton(); | |
307 | ||
308 | if ($dateParts) { | |
be2fb01f | 309 | if (array_intersect(['h', 'H'], $dateParts)) { |
6a488035 TO |
310 | $format = $config->dateformatDatetime; |
311 | } | |
be2fb01f | 312 | elseif (array_intersect(['d', 'j'], $dateParts)) { |
6a488035 TO |
313 | $format = $config->dateformatFull; |
314 | } | |
be2fb01f | 315 | elseif (array_intersect(['m', 'M'], $dateParts)) { |
6a488035 TO |
316 | $format = $config->dateformatPartial; |
317 | } | |
318 | else { | |
319 | $format = $config->dateformatYear; | |
320 | } | |
321 | } | |
322 | else { | |
323 | if (strpos($dateString, '-')) { | |
324 | $month = (int) substr($dateString, 5, 2); | |
325 | $day = (int) substr($dateString, 8, 2); | |
326 | } | |
327 | else { | |
328 | $month = (int) substr($dateString, 4, 2); | |
329 | $day = (int) substr($dateString, 6, 2); | |
330 | } | |
331 | ||
332 | if (strlen($dateString) > 10) { | |
333 | $format = $config->dateformatDatetime; | |
334 | } | |
335 | elseif ($day > 0) { | |
336 | $format = $config->dateformatFull; | |
337 | } | |
338 | elseif ($month > 0) { | |
339 | $format = $config->dateformatPartial; | |
340 | } | |
341 | else { | |
342 | $format = $config->dateformatYear; | |
343 | } | |
344 | } | |
345 | } | |
346 | ||
35fa23f8 | 347 | if (!CRM_Utils_System::isNull($dateString)) { |
6a488035 | 348 | if (strpos($dateString, '-')) { |
353ffa53 | 349 | $year = (int) substr($dateString, 0, 4); |
6a488035 | 350 | $month = (int) substr($dateString, 5, 2); |
353ffa53 | 351 | $day = (int) substr($dateString, 8, 2); |
6a488035 TO |
352 | |
353 | $hour24 = (int) substr($dateString, 11, 2); | |
354 | $minute = (int) substr($dateString, 14, 2); | |
355 | } | |
356 | else { | |
353ffa53 | 357 | $year = (int) substr($dateString, 0, 4); |
6a488035 | 358 | $month = (int) substr($dateString, 4, 2); |
353ffa53 | 359 | $day = (int) substr($dateString, 6, 2); |
6a488035 TO |
360 | |
361 | $hour24 = (int) substr($dateString, 8, 2); | |
362 | $minute = (int) substr($dateString, 10, 2); | |
363 | } | |
364 | ||
365 | if ($day % 10 == 1 and $day != 11) { | |
366 | $suffix = 'st'; | |
367 | } | |
368 | elseif ($day % 10 == 2 and $day != 12) { | |
369 | $suffix = 'nd'; | |
370 | } | |
371 | elseif ($day % 10 == 3 and $day != 13) { | |
372 | $suffix = 'rd'; | |
373 | } | |
374 | else { | |
375 | $suffix = 'th'; | |
376 | } | |
377 | ||
378 | if ($hour24 < 12) { | |
379 | if ($hour24 == 00) { | |
380 | $hour12 = 12; | |
381 | } | |
382 | else { | |
383 | $hour12 = $hour24; | |
384 | } | |
385 | $type = 'AM'; | |
386 | } | |
387 | else { | |
388 | if ($hour24 == 12) { | |
389 | $hour12 = 12; | |
390 | } | |
391 | else { | |
392 | $hour12 = $hour24 - 12; | |
393 | } | |
394 | $type = 'PM'; | |
395 | } | |
396 | ||
be2fb01f | 397 | $date = [ |
6b409353 CW |
398 | '%b' => $abbrMonths[$month] ?? NULL, |
399 | '%B' => $fullMonths[$month] ?? NULL, | |
6a488035 TO |
400 | '%d' => $day > 9 ? $day : '0' . $day, |
401 | '%e' => $day > 9 ? $day : ' ' . $day, | |
402 | '%E' => $day, | |
403 | '%f' => $suffix, | |
404 | '%H' => $hour24 > 9 ? $hour24 : '0' . $hour24, | |
405 | '%h' => $hour12 > 9 ? $hour12 : '0' . $hour12, | |
406 | '%I' => $hour12 > 9 ? $hour12 : '0' . $hour12, | |
407 | '%k' => $hour24 > 9 ? $hour24 : ' ' . $hour24, | |
408 | '%l' => $hour12 > 9 ? $hour12 : ' ' . $hour12, | |
409 | '%m' => $month > 9 ? $month : '0' . $month, | |
410 | '%M' => $minute > 9 ? $minute : '0' . $minute, | |
411 | '%i' => $minute > 9 ? $minute : '0' . $minute, | |
412 | '%p' => strtolower($type), | |
413 | '%P' => $type, | |
414 | '%A' => $type, | |
415 | '%Y' => $year, | |
be2fb01f | 416 | ]; |
6a488035 TO |
417 | |
418 | return strtr($format, $date); | |
419 | } | |
420 | else { | |
421 | return ''; | |
422 | } | |
423 | } | |
424 | ||
d2f262da | 425 | /** |
56f54f02 | 426 | * Format the field according to the site's preferred date format. |
427 | * | |
428 | * This is likely to look something like December 31st, 2020. | |
429 | * | |
430 | * @param string $date | |
431 | * | |
432 | * @return string | |
433 | */ | |
434 | public static function formatDateOnlyLong(string $date):string { | |
435 | return CRM_Utils_Date::customFormat($date, Civi::settings()->get('dateformatFull')); | |
436 | } | |
437 | ||
438 | /** | |
d2f262da AS |
439 | * Wrapper for customFormat that takes a timestamp |
440 | * | |
441 | * @param int $timestamp | |
442 | * Date and time in timestamp format. | |
443 | * @param string $format | |
444 | * The output format. | |
445 | * @param array $dateParts | |
446 | * An array with the desired date parts. | |
447 | * | |
448 | * @return string | |
449 | * the $format-formatted $date | |
450 | */ | |
451 | public static function customFormatTs($timestamp, $format = NULL, $dateParts = NULL) { | |
452 | return CRM_Utils_Date::customFormat(date("Y-m-d H:i:s", $timestamp), $format, $dateParts); | |
453 | } | |
454 | ||
6a488035 | 455 | /** |
100fef9d | 456 | * Converts the date/datetime from MySQL format to ISO format |
6a488035 | 457 | * |
77855840 TO |
458 | * @param string $mysql |
459 | * Date/datetime in MySQL format. | |
6a488035 | 460 | * |
a6c01b45 CW |
461 | * @return string |
462 | * date/datetime in ISO format | |
6a488035 | 463 | */ |
00be9182 | 464 | public static function mysqlToIso($mysql) { |
353ffa53 TO |
465 | $year = substr($mysql, 0, 4); |
466 | $month = substr($mysql, 4, 2); | |
467 | $day = substr($mysql, 6, 2); | |
468 | $hour = substr($mysql, 8, 2); | |
6a488035 TO |
469 | $minute = substr($mysql, 10, 2); |
470 | $second = substr($mysql, 12, 2); | |
471 | ||
472 | $iso = ''; | |
473 | if ($year) { | |
474 | $iso .= "$year"; | |
475 | } | |
476 | if ($month) { | |
477 | $iso .= "-$month"; | |
478 | if ($day) { | |
479 | $iso .= "-$day"; | |
480 | } | |
481 | } | |
482 | ||
483 | if ($hour) { | |
484 | $iso .= " $hour"; | |
485 | if ($minute) { | |
486 | $iso .= ":$minute"; | |
487 | if ($second) { | |
488 | $iso .= ":$second"; | |
489 | } | |
490 | } | |
491 | } | |
492 | return $iso; | |
493 | } | |
494 | ||
495 | /** | |
100fef9d | 496 | * Converts the date/datetime from ISO format to MySQL format |
5d9f6898 EM |
497 | * Note that until CRM-14986/ 4.4.7 this was required whenever the pattern $dao->find(TRUE): $dao->save(); was |
498 | * used to update an object with a date field was used. The DAO now checks for a '-' in date field strings | |
499 | * & runs this function if the - appears - meaning it is likely redundant in the form & BAO layers | |
6a488035 | 500 | * |
77855840 TO |
501 | * @param string $iso |
502 | * Date/datetime in ISO format. | |
6a488035 | 503 | * |
a6c01b45 CW |
504 | * @return string |
505 | * date/datetime in MySQL format | |
6a488035 | 506 | */ |
00be9182 | 507 | public static function isoToMysql($iso) { |
be2fb01f | 508 | $dropArray = ['-' => '', ':' => '', ' ' => '']; |
6a488035 TO |
509 | return strtr($iso, $dropArray); |
510 | } | |
511 | ||
512 | /** | |
100fef9d | 513 | * Converts the any given date to default date format. |
6a488035 | 514 | * |
77855840 TO |
515 | * @param array $params |
516 | * Has given date-format. | |
517 | * @param int $dateType | |
518 | * Type of date. | |
519 | * @param string $dateParam | |
520 | * Index of params. | |
f4aaa82a EM |
521 | * |
522 | * @return bool | |
6a488035 | 523 | */ |
00be9182 | 524 | public static function convertToDefaultDate(&$params, $dateType, $dateParam) { |
353ffa53 TO |
525 | $now = getdate(); |
526 | $cen = substr($now['year'], 0, 2); | |
6a488035 TO |
527 | $prevCen = $cen - 1; |
528 | ||
529 | $value = NULL; | |
a7488080 | 530 | if (!empty($params[$dateParam])) { |
6a488035 TO |
531 | // suppress hh:mm or hh:mm:ss if it exists CRM-7957 |
532 | $value = preg_replace("/(\s(([01]\d)|[2][0-3])(:([0-5]\d)){1,2})$/", "", $params[$dateParam]); | |
533 | } | |
534 | ||
535 | switch ($dateType) { | |
536 | case 1: | |
537 | if (!preg_match('/^\d\d\d\d-?(\d|\d\d)-?(\d|\d\d)$/', $value)) { | |
538 | return FALSE; | |
539 | } | |
540 | break; | |
541 | ||
542 | case 2: | |
543 | if (!preg_match('/^(\d|\d\d)[-\/](\d|\d\d)[-\/]\d\d$/', $value)) { | |
544 | return FALSE; | |
545 | } | |
546 | break; | |
547 | ||
548 | case 4: | |
549 | if (!preg_match('/^(\d|\d\d)[-\/](\d|\d\d)[-\/]\d\d\d\d$/', $value)) { | |
550 | return FALSE; | |
551 | } | |
552 | break; | |
553 | ||
554 | case 8: | |
555 | if (!preg_match('/^[A-Za-z]*.[ \t]?\d\d\,[ \t]?\d\d\d\d$/', $value)) { | |
556 | return FALSE; | |
557 | } | |
558 | break; | |
559 | ||
560 | case 16: | |
561 | if (!preg_match('/^\d\d-[A-Za-z]{3}.*-\d\d$/', $value) && !preg_match('/^\d\d[-\/]\d\d[-\/]\d\d$/', $value)) { | |
562 | return FALSE; | |
563 | } | |
564 | break; | |
565 | ||
566 | case 32: | |
567 | if (!preg_match('/^(\d|\d\d)[-\/](\d|\d\d)[-\/]\d\d\d\d/', $value)) { | |
568 | return FALSE; | |
569 | } | |
570 | break; | |
571 | } | |
572 | ||
573 | if ($dateType == 1) { | |
574 | $formattedDate = explode("-", $value); | |
575 | if (count($formattedDate) == 3) { | |
353ffa53 | 576 | $year = (int) $formattedDate[0]; |
6a488035 | 577 | $month = (int) $formattedDate[1]; |
353ffa53 | 578 | $day = (int) $formattedDate[2]; |
6a488035 TO |
579 | } |
580 | elseif (count($formattedDate) == 1 && (strlen($value) == 8)) { | |
581 | return TRUE; | |
582 | } | |
583 | else { | |
584 | return FALSE; | |
585 | } | |
586 | } | |
587 | ||
6a488035 TO |
588 | if ($dateType == 2 || $dateType == 4) { |
589 | $formattedDate = explode("/", $value); | |
590 | if (count($formattedDate) != 3) { | |
591 | $formattedDate = explode("-", $value); | |
592 | } | |
593 | if (count($formattedDate) == 3) { | |
353ffa53 | 594 | $year = (int) $formattedDate[2]; |
6a488035 | 595 | $month = (int) $formattedDate[0]; |
353ffa53 | 596 | $day = (int) $formattedDate[1]; |
6a488035 TO |
597 | } |
598 | else { | |
599 | return FALSE; | |
600 | } | |
601 | } | |
602 | if ($dateType == 8) { | |
603 | $dateArray = explode(' ', $value); | |
604 | // ignore comma(,) | |
605 | $dateArray[1] = (int) substr($dateArray[1], 0, 2); | |
606 | ||
607 | $monthInt = 0; | |
608 | $fullMonths = self::getFullMonthNames(); | |
609 | foreach ($fullMonths as $key => $val) { | |
610 | if (strtolower($dateArray[0]) == strtolower($val)) { | |
611 | $monthInt = $key; | |
612 | break; | |
613 | } | |
614 | } | |
615 | if (!$monthInt) { | |
616 | $abbrMonths = self::getAbbrMonthNames(); | |
617 | foreach ($abbrMonths as $key => $val) { | |
618 | if (strtolower(trim($dateArray[0], ".")) == strtolower($val)) { | |
619 | $monthInt = $key; | |
620 | break; | |
621 | } | |
622 | } | |
623 | } | |
353ffa53 TO |
624 | $year = (int) $dateArray[2]; |
625 | $day = (int) $dateArray[1]; | |
6a488035 TO |
626 | $month = (int) $monthInt; |
627 | } | |
628 | if ($dateType == 16) { | |
629 | $dateArray = explode('-', $value); | |
630 | if (count($dateArray) != 3) { | |
631 | $dateArray = explode('/', $value); | |
632 | } | |
633 | ||
634 | if (count($dateArray) == 3) { | |
635 | $monthInt = 0; | |
636 | $fullMonths = self::getFullMonthNames(); | |
637 | foreach ($fullMonths as $key => $val) { | |
638 | if (strtolower($dateArray[1]) == strtolower($val)) { | |
639 | $monthInt = $key; | |
640 | break; | |
641 | } | |
642 | } | |
643 | if (!$monthInt) { | |
644 | $abbrMonths = self::getAbbrMonthNames(); | |
645 | foreach ($abbrMonths as $key => $val) { | |
646 | if (strtolower(trim($dateArray[1], ".")) == strtolower($val)) { | |
647 | $monthInt = $key; | |
648 | break; | |
649 | } | |
650 | } | |
651 | } | |
652 | if (!$monthInt) { | |
653 | $monthInt = $dateArray[1]; | |
654 | } | |
655 | ||
353ffa53 TO |
656 | $year = (int) $dateArray[2]; |
657 | $day = (int) $dateArray[0]; | |
6a488035 TO |
658 | $month = (int) $monthInt; |
659 | } | |
660 | else { | |
661 | return FALSE; | |
662 | } | |
663 | } | |
664 | if ($dateType == 32) { | |
665 | $formattedDate = explode("/", $value); | |
666 | if (count($formattedDate) == 3) { | |
353ffa53 | 667 | $year = (int) $formattedDate[2]; |
6a488035 | 668 | $month = (int) $formattedDate[1]; |
353ffa53 | 669 | $day = (int) $formattedDate[0]; |
6a488035 TO |
670 | } |
671 | else { | |
672 | return FALSE; | |
673 | } | |
674 | } | |
675 | ||
676 | $month = ($month < 10) ? "0" . "$month" : $month; | |
677 | $day = ($day < 10) ? "0" . "$day" : $day; | |
678 | ||
679 | $year = (int ) $year; | |
680 | // simple heuristic to determine what century to use | |
681 | // 00 - 20 is always 2000 - 2020 | |
682 | // 21 - 99 is always 1921 - 1999 | |
683 | if ($year < 21) { | |
684 | $year = (strlen($year) == 1) ? $cen . '0' . $year : $cen . $year; | |
685 | } | |
686 | elseif ($year < 100) { | |
687 | $year = $prevCen . $year; | |
688 | } | |
689 | ||
690 | if ($params[$dateParam]) { | |
691 | $params[$dateParam] = "$year$month$day"; | |
692 | } | |
50bfb460 | 693 | // if month is invalid return as error |
6a488035 TO |
694 | if ($month !== '00' && $month <= 12) { |
695 | return TRUE; | |
696 | } | |
697 | return FALSE; | |
698 | } | |
699 | ||
5bc392e6 EM |
700 | /** |
701 | * @param $date | |
702 | * | |
703 | * @return bool | |
704 | */ | |
00be9182 | 705 | public static function isDate(&$date) { |
6a488035 TO |
706 | if (CRM_Utils_System::isNull($date)) { |
707 | return FALSE; | |
708 | } | |
709 | return TRUE; | |
710 | } | |
711 | ||
e73e7ec8 TO |
712 | /** |
713 | * Translate a TTL to a concrete expiration time. | |
714 | * | |
715 | * @param NULL|int|DateInterval $ttl | |
716 | * @param int $default | |
717 | * The value to use if $ttl is not specified (NULL). | |
718 | * @return int | |
719 | * Timestamp (seconds since epoch). | |
720 | * @throws \CRM_Utils_Cache_InvalidArgumentException | |
721 | */ | |
722 | public static function convertCacheTtlToExpires($ttl, $default) { | |
723 | if ($ttl === NULL) { | |
724 | $ttl = $default; | |
725 | } | |
726 | ||
727 | if (is_int($ttl)) { | |
728 | return time() + $ttl; | |
729 | } | |
730 | elseif ($ttl instanceof DateInterval) { | |
731 | return date_add(new DateTime(), $ttl)->getTimestamp(); | |
732 | } | |
733 | else { | |
734 | throw new CRM_Utils_Cache_InvalidArgumentException("Invalid cache TTL"); | |
735 | } | |
736 | } | |
737 | ||
738 | /** | |
739 | * Normalize a TTL. | |
740 | * | |
741 | * @param NULL|int|DateInterval $ttl | |
742 | * @param int $default | |
743 | * The value to use if $ttl is not specified (NULL). | |
744 | * @return int | |
745 | * Seconds until expiration. | |
746 | * @throws \CRM_Utils_Cache_InvalidArgumentException | |
747 | */ | |
748 | public static function convertCacheTtl($ttl, $default) { | |
749 | if ($ttl === NULL) { | |
750 | return $default; | |
751 | } | |
752 | elseif (is_int($ttl)) { | |
753 | return $ttl; | |
754 | } | |
755 | elseif ($ttl instanceof DateInterval) { | |
756 | return date_add(new DateTime(), $ttl)->getTimestamp() - time(); | |
757 | } | |
758 | else { | |
759 | throw new CRM_Utils_Cache_InvalidArgumentException("Invalid cache TTL"); | |
760 | } | |
761 | } | |
762 | ||
5bc392e6 EM |
763 | /** |
764 | * @param null $timeStamp | |
765 | * | |
766 | * @return bool|string | |
767 | */ | |
00be9182 | 768 | public static function currentDBDate($timeStamp = NULL) { |
6a488035 TO |
769 | return $timeStamp ? date('YmdHis', $timeStamp) : date('YmdHis'); |
770 | } | |
771 | ||
5bc392e6 EM |
772 | /** |
773 | * @param $date | |
774 | * @param null $now | |
775 | * | |
776 | * @return bool | |
777 | */ | |
00be9182 | 778 | public static function overdue($date, $now = NULL) { |
6a488035 TO |
779 | $mysqlDate = self::isoToMysql($date); |
780 | if (!$now) { | |
781 | $now = self::currentDBDate(); | |
782 | } | |
783 | else { | |
784 | $now = self::isoToMysql($now); | |
785 | } | |
786 | ||
91768280 | 787 | return !(strtotime($mysqlDate) >= strtotime($now)); |
6a488035 TO |
788 | } |
789 | ||
790 | /** | |
fe482240 | 791 | * Get customized today. |
6a488035 TO |
792 | * |
793 | * This function is used for getting customized today. To get | |
794 | * actuall today pass 'dayParams' as null. or else pass the day, | |
795 | * month, year values as array values | |
796 | * Example: $dayParams = array( | |
353ffa53 | 797 | * 'day' => '25', 'month' => '10', |
6a488035 TO |
798 | * 'year' => '2007' ); |
799 | * | |
d5cc0fc2 | 800 | * @param array $dayParams of the day, month, year. |
77855840 | 801 | * Array of the day, month, year. |
6a488035 | 802 | * values. |
77855840 TO |
803 | * @param string $format |
804 | * Expected date format( default. | |
6a488035 TO |
805 | * format is 2007-12-21 ) |
806 | * | |
a6c01b45 | 807 | * @return string |
b44e3f84 | 808 | * Return the customized today's date (Y-m-d) |
6a488035 | 809 | */ |
00be9182 | 810 | public static function getToday($dayParams = NULL, $format = "Y-m-d") { |
6a488035 TO |
811 | if (is_null($dayParams) || empty($dayParams)) { |
812 | $today = date($format); | |
813 | } | |
814 | else { | |
815 | $today = date($format, mktime(0, 0, 0, | |
353ffa53 TO |
816 | $dayParams['month'], |
817 | $dayParams['day'], | |
818 | $dayParams['year'] | |
819 | )); | |
6a488035 TO |
820 | } |
821 | ||
822 | return $today; | |
823 | } | |
824 | ||
825 | /** | |
100fef9d | 826 | * Find whether today's date lies in |
6a488035 TO |
827 | * the given range |
828 | * | |
77855840 TO |
829 | * @param date $startDate |
830 | * Start date for the range. | |
831 | * @param date $endDate | |
832 | * End date for the range. | |
6a488035 | 833 | * |
72b3a70c CW |
834 | * @return bool |
835 | * true if today's date is in the given date range | |
6a488035 | 836 | */ |
00be9182 | 837 | public static function getRange($startDate, $endDate) { |
353ffa53 | 838 | $today = date("Y-m-d"); |
6a488035 | 839 | $mysqlStartDate = self::isoToMysql($startDate); |
353ffa53 TO |
840 | $mysqlEndDate = self::isoToMysql($endDate); |
841 | $mysqlToday = self::isoToMysql($today); | |
6a488035 TO |
842 | |
843 | if ((isset($mysqlStartDate) && isset($mysqlEndDate)) && (($mysqlToday >= $mysqlStartDate) && ($mysqlToday <= $mysqlEndDate))) { | |
844 | return TRUE; | |
845 | } | |
846 | elseif ((isset($mysqlStartDate) && !isset($mysqlEndDate)) && (($mysqlToday >= $mysqlStartDate))) { | |
847 | return TRUE; | |
848 | } | |
849 | elseif ((!isset($mysqlStartDate) && isset($mysqlEndDate)) && (($mysqlToday <= $mysqlEndDate))) { | |
850 | return TRUE; | |
851 | } | |
852 | return FALSE; | |
853 | } | |
854 | ||
855 | /** | |
100fef9d | 856 | * Get start date and end from |
6a488035 TO |
857 | * the given relative term and unit |
858 | * | |
7ec58d2b J |
859 | * @param string $relative Relative format in the format term.unit. |
860 | * Eg: previous.day | |
f4aaa82a | 861 | * |
819e6707 J |
862 | * @param string $from |
863 | * @param string $to | |
864 | * @param string $fromTime | |
865 | * @param string $toTime | |
6a488035 | 866 | * |
a6c01b45 CW |
867 | * @return array |
868 | * start date, end date | |
6a488035 | 869 | */ |
3dd9e4a0 | 870 | public static function getFromTo($relative, $from = NULL, $to = NULL, $fromTime = NULL, $toTime = '235959') { |
6a488035 | 871 | if ($relative) { |
819e6707 | 872 | list($term, $unit) = explode('.', $relative, 2); |
6a488035 | 873 | $dateRange = self::relativeToAbsolute($term, $unit); |
819e6707 | 874 | $from = substr($dateRange['from'], 0, 8); |
6a488035 | 875 | $to = substr($dateRange['to'], 0, 8); |
7ec58d2b J |
876 | // @todo fix relativeToAbsolute & add tests |
877 | // relativeToAbsolute returns 8 char date strings | |
878 | // or 14 char date + time strings. | |
879 | // We should use those. However, it turns out to be unreliable. | |
880 | // e.g. this.week does NOT return 235959 for 'from' | |
881 | // so our defaults are more reliable. | |
882 | // Currently relativeToAbsolute only supports 'whole' days so that is ok | |
6a488035 TO |
883 | } |
884 | ||
819e6707 J |
885 | $from = self::processDate($from, $fromTime); |
886 | $to = self::processDate($to, $toTime); | |
6a488035 | 887 | |
be2fb01f | 888 | return [$from, $to]; |
6a488035 TO |
889 | } |
890 | ||
891 | /** | |
fe482240 | 892 | * Calculate Age in Years if greater than one year else in months. |
6a488035 | 893 | * |
77855840 TO |
894 | * @param date $birthDate |
895 | * Birth Date. | |
786bde06 SP |
896 | * @param date $targetDate |
897 | * Target Date. (show age on specific date) | |
6a488035 | 898 | * |
a6c01b45 CW |
899 | * @return int |
900 | * array $results contains years or months | |
6a488035 | 901 | */ |
786bde06 | 902 | public static function calculateAge($birthDate, $targetDate = NULL) { |
be2fb01f | 903 | $results = []; |
6a488035 TO |
904 | $formatedBirthDate = CRM_Utils_Date::customFormat($birthDate, '%Y-%m-%d'); |
905 | ||
353ffa53 TO |
906 | $bDate = explode('-', $formatedBirthDate); |
907 | $birthYear = $bDate[0]; | |
6a488035 | 908 | $birthMonth = $bDate[1]; |
353ffa53 | 909 | $birthDay = $bDate[2]; |
6004d2f7 | 910 | $targetDate = strtotime($targetDate ?? date('Y-m-d')); |
786bde06 SP |
911 | |
912 | $year_diff = date("Y", $targetDate) - $birthYear; | |
6a488035 TO |
913 | |
914 | // don't calculate age CRM-3143 | |
915 | if ($birthYear == '1902') { | |
916 | return $results; | |
917 | } | |
6a488035 TO |
918 | switch ($year_diff) { |
919 | case 1: | |
786bde06 | 920 | $month = (12 - $birthMonth) + date("m", $targetDate); |
6a488035 | 921 | if ($month < 12) { |
786bde06 | 922 | if (date("d", $targetDate) < $birthDay) { |
6a488035 TO |
923 | $month--; |
924 | } | |
925 | $results['months'] = $month; | |
926 | } | |
786bde06 | 927 | elseif ($month == 12 && (date("d", $targetDate) < $birthDay)) { |
6a488035 TO |
928 | $results['months'] = $month - 1; |
929 | } | |
930 | else { | |
931 | $results['years'] = $year_diff; | |
932 | } | |
933 | break; | |
934 | ||
935 | case 0: | |
786bde06 | 936 | $month = date("m", $targetDate) - $birthMonth; |
6a488035 TO |
937 | $results['months'] = $month; |
938 | break; | |
939 | ||
940 | default: | |
941 | $results['years'] = $year_diff; | |
786bde06 | 942 | if ((date("m", $targetDate) < $birthMonth) || (date("m", $targetDate) == $birthMonth) && (date("d", $targetDate) < $birthDay)) { |
6a488035 TO |
943 | $results['years']--; |
944 | } | |
945 | } | |
946 | ||
947 | return $results; | |
948 | } | |
949 | ||
950 | /** | |
100fef9d | 951 | * Calculate next payment date according to provided unit & interval |
6a488035 | 952 | * |
77855840 TO |
953 | * @param string $unit |
954 | * Frequency unit like year,month, week etc. | |
6a488035 | 955 | * |
77855840 TO |
956 | * @param int $interval |
957 | * Frequency interval. | |
6a488035 | 958 | * |
77855840 TO |
959 | * @param array $date |
960 | * Start date of pledge. | |
f4aaa82a EM |
961 | * |
962 | * @param bool $dontCareTime | |
6a488035 | 963 | * |
a6c01b45 CW |
964 | * @return array |
965 | * contains new date with added interval | |
6a488035 | 966 | */ |
00be9182 | 967 | public static function intervalAdd($unit, $interval, $date, $dontCareTime = FALSE) { |
6a488035 | 968 | if (is_array($date)) { |
4f758891 SL |
969 | $hour = $date['H'] ?? '00'; |
970 | $minute = $date['i'] ?? '00'; | |
971 | $second = $date['s'] ?? '00'; | |
9c1bc317 CW |
972 | $month = $date['M'] ?? NULL; |
973 | $day = $date['d'] ?? NULL; | |
974 | $year = $date['Y'] ?? NULL; | |
6a488035 TO |
975 | } |
976 | else { | |
977 | extract(date_parse($date)); | |
978 | } | |
979 | $date = mktime($hour, $minute, $second, $month, $day, $year); | |
980 | switch ($unit) { | |
981 | case 'year': | |
982 | $date = mktime($hour, $minute, $second, $month, $day, $year + $interval); | |
983 | break; | |
984 | ||
985 | case 'month': | |
986 | $date = mktime($hour, $minute, $second, $month + $interval, $day, $year); | |
987 | break; | |
988 | ||
989 | case 'week': | |
990 | $interval = $interval * 7; | |
991 | $date = mktime($hour, $minute, $second, $month, $day + $interval, $year); | |
992 | break; | |
993 | ||
994 | case 'day': | |
995 | $date = mktime($hour, $minute, $second, $month, $day + $interval, $year); | |
996 | break; | |
997 | ||
998 | case 'second': | |
999 | $date = mktime($hour, $minute, $second + $interval, $month, $day, $year); | |
1000 | break; | |
1001 | } | |
1002 | ||
1003 | $scheduleDate = explode("-", date("n-j-Y-H-i-s", $date)); | |
1004 | ||
be2fb01f | 1005 | $date = []; |
6a488035 TO |
1006 | $date['M'] = $scheduleDate[0]; |
1007 | $date['d'] = $scheduleDate[1]; | |
1008 | $date['Y'] = $scheduleDate[2]; | |
1009 | if ($dontCareTime == FALSE) { | |
1010 | $date['H'] = $scheduleDate[3]; | |
1011 | $date['i'] = $scheduleDate[4]; | |
1012 | $date['s'] = $scheduleDate[5]; | |
1013 | } | |
1014 | return $date; | |
1015 | } | |
1016 | ||
1017 | /** | |
ed0ca248 | 1018 | * Get the smarty view presentation mapping for the given format. |
1019 | * | |
1020 | * Historically it was decided that where the view format is 'dd/mm/yy' or 'mm/dd/yy' | |
1021 | * they should be rendered using a longer date format. This is likely as much to | |
1022 | * do with the earlier date widget being unable to handle some formats as usablity. | |
1023 | * However, we continue to respect this. | |
6a488035 | 1024 | * |
77855840 TO |
1025 | * @param $format |
1026 | * Given format ( eg 'M Y', 'Y M' ). | |
f4aaa82a | 1027 | * |
ed0ca248 | 1028 | * @return string|null |
1029 | * Smarty translation of the date format. Null is also valid and is translated | |
1030 | * according to the available parts at the smarty layer. | |
6a488035 | 1031 | */ |
ed0ca248 | 1032 | public static function getDateFieldViewFormat($format) { |
be2fb01f | 1033 | $supportableFormats = [ |
6a488035 TO |
1034 | 'mm/dd' => '%B %E%f', |
1035 | 'dd-mm' => '%E%f %B', | |
1036 | 'yy-mm' => '%Y %B', | |
1037 | 'M yy' => '%b %Y', | |
1038 | 'yy' => '%Y', | |
1039 | 'dd/mm/yy' => '%E%f %B %Y', | |
be2fb01f | 1040 | ]; |
6a488035 | 1041 | |
ed0ca248 | 1042 | return array_key_exists($format, $supportableFormats) ? $supportableFormats[$format] : self::pickBestSmartyFormat($format); |
1043 | } | |
1044 | ||
1045 | /** | |
1046 | * Pick the smarty format from settings that best matches the time string we have. | |
1047 | * | |
1048 | * For view purposes we historically use the setting that most closely matches the data | |
1049 | * in the format from our settings, as opposed to the setting configured for the field. | |
1050 | * | |
1051 | * @param $format | |
1052 | * @return mixed | |
1053 | */ | |
1054 | public static function pickBestSmartyFormat($format) { | |
1055 | if (stristr($format, 'h')) { | |
1056 | return Civi::settings()->get('dateformatDatetime'); | |
1057 | } | |
1058 | if (stristr($format, 'd') || stristr($format, 'j')) { | |
1059 | return Civi::settings()->get('dateformatFull'); | |
1060 | } | |
1061 | if (stristr($format, 'm')) { | |
1062 | return Civi::settings()->get('dateformatPartial'); | |
6a488035 | 1063 | } |
ed0ca248 | 1064 | return Civi::settings()->get('dateformatYear'); |
1065 | } | |
6a488035 | 1066 | |
ed0ca248 | 1067 | /** |
1068 | * Map date plugin and actual format that is used by PHP. | |
1069 | * | |
1070 | * @return array | |
1071 | */ | |
1072 | public static function datePluginToPHPFormats() { | |
be2fb01f | 1073 | $dateInputFormats = [ |
ed0ca248 | 1074 | "mm/dd/yy" => 'm/d/Y', |
1075 | "dd/mm/yy" => 'd/m/Y', | |
1076 | "yy-mm-dd" => 'Y-m-d', | |
1077 | "dd-mm-yy" => 'd-m-Y', | |
1078 | "dd.mm.yy" => 'd.m.Y', | |
1079 | "M d" => 'M j', | |
1080 | "M d, yy" => 'M j, Y', | |
1081 | "d M yy" => 'j M Y', | |
1082 | "MM d, yy" => 'F j, Y', | |
1083 | "d MM yy" => 'j F Y', | |
1084 | "DD, d MM yy" => 'l, j F Y', | |
1085 | "mm/dd" => 'm/d', | |
1086 | "dd-mm" => 'd-m', | |
1087 | "yy-mm" => 'Y-m', | |
1088 | "M yy" => 'M Y', | |
2732685b | 1089 | "M Y" => 'M Y', |
ed0ca248 | 1090 | "yy" => 'Y', |
be2fb01f | 1091 | ]; |
ed0ca248 | 1092 | return $dateInputFormats; |
6a488035 TO |
1093 | } |
1094 | ||
1095 | /** | |
fe482240 | 1096 | * Resolves the given relative time interval into finite time limits. |
6a488035 | 1097 | * |
1e60a380 | 1098 | * @param string $relativeTerm |
1099 | * Relative time frame: this, previous, previous_1. | |
77855840 TO |
1100 | * @param int $unit |
1101 | * Frequency unit like year, month, week etc. | |
6a488035 | 1102 | * |
a6c01b45 CW |
1103 | * @return array |
1104 | * start date and end date for the relative time frame | |
6a488035 | 1105 | */ |
00be9182 | 1106 | public static function relativeToAbsolute($relativeTerm, $unit) { |
353ffa53 | 1107 | $now = getdate(); |
be2fb01f | 1108 | $from = $to = $dateRange = []; |
6a488035 | 1109 | $from['H'] = $from['i'] = $from['s'] = 0; |
1e60a380 | 1110 | $relativeTermParts = explode('_', $relativeTerm); |
1111 | $relativeTermPrefix = $relativeTermParts[0]; | |
2e1f50d6 | 1112 | $relativeTermSuffix = $relativeTermParts[1] ?? ''; |
6a488035 TO |
1113 | |
1114 | switch ($unit) { | |
1115 | case 'year': | |
1116 | switch ($relativeTerm) { | |
6a488035 TO |
1117 | case 'previous': |
1118 | $from['M'] = $from['d'] = 1; | |
353ffa53 TO |
1119 | $to['d'] = 31; |
1120 | $to['M'] = 12; | |
1121 | $to['Y'] = $from['Y'] = $now['year'] - 1; | |
6a488035 TO |
1122 | break; |
1123 | ||
1124 | case 'previous_before': | |
1125 | $from['M'] = $from['d'] = 1; | |
353ffa53 TO |
1126 | $to['d'] = 31; |
1127 | $to['M'] = 12; | |
1128 | $to['Y'] = $from['Y'] = $now['year'] - 2; | |
6a488035 TO |
1129 | break; |
1130 | ||
1131 | case 'previous_2': | |
1132 | $from['M'] = $from['d'] = 1; | |
353ffa53 TO |
1133 | $to['d'] = 31; |
1134 | $to['M'] = 12; | |
6a488035 | 1135 | $from['Y'] = $now['year'] - 2; |
353ffa53 | 1136 | $to['Y'] = $now['year'] - 1; |
6a488035 TO |
1137 | break; |
1138 | ||
1139 | case 'earlier': | |
1140 | $to['d'] = 31; | |
1141 | $to['M'] = 12; | |
1142 | $to['Y'] = $now['year'] - 1; | |
1143 | unset($from); | |
1144 | break; | |
1145 | ||
1146 | case 'greater': | |
1147 | $from['M'] = $from['d'] = 1; | |
1148 | $from['Y'] = $now['year']; | |
1149 | unset($to); | |
1150 | break; | |
1151 | ||
52f15bd6 | 1152 | case 'greater_previous': |
1153 | $from['d'] = 31; | |
1154 | $from['M'] = 12; | |
1155 | $from['Y'] = $now['year'] - 1; | |
1156 | unset($to); | |
1157 | break; | |
1158 | ||
6a488035 TO |
1159 | case 'ending': |
1160 | $to['d'] = $now['mday']; | |
1161 | $to['M'] = $now['mon']; | |
1162 | $to['Y'] = $now['year']; | |
1163 | $to['H'] = 23; | |
1164 | $to['i'] = $to['s'] = 59; | |
353ffa53 TO |
1165 | $from = self::intervalAdd('year', -1, $to); |
1166 | $from = self::intervalAdd('second', 1, $from); | |
6a488035 | 1167 | break; |
e902edd1 | 1168 | |
1169 | case 'current': | |
1170 | $from['M'] = $from['d'] = 1; | |
1171 | $from['Y'] = $now['year']; | |
1172 | $to['H'] = 23; | |
1173 | $to['i'] = $to['s'] = 59; | |
1174 | $to['d'] = $now['mday']; | |
1175 | $to['M'] = $now['mon']; | |
1176 | $to['Y'] = $now['year']; | |
1177 | break; | |
e2c3163d | 1178 | |
1179 | case 'ending_2': | |
1180 | $to['d'] = $now['mday']; | |
1181 | $to['M'] = $now['mon']; | |
1182 | $to['Y'] = $now['year']; | |
1183 | $to['H'] = 23; | |
1184 | $to['i'] = $to['s'] = 59; | |
353ffa53 TO |
1185 | $from = self::intervalAdd('year', -2, $to); |
1186 | $from = self::intervalAdd('second', 1, $from); | |
e2c3163d | 1187 | break; |
1188 | ||
1189 | case 'ending_3': | |
1190 | $to['d'] = $now['mday']; | |
1191 | $to['M'] = $now['mon']; | |
1192 | $to['Y'] = $now['year']; | |
1193 | $to['H'] = 23; | |
1194 | $to['i'] = $to['s'] = 59; | |
353ffa53 TO |
1195 | $from = self::intervalAdd('year', -3, $to); |
1196 | $from = self::intervalAdd('second', 1, $from); | |
e2c3163d | 1197 | break; |
c5670e9e | 1198 | |
1199 | case 'less': | |
353ffa53 TO |
1200 | $to['d'] = 31; |
1201 | $to['M'] = 12; | |
1202 | $to['Y'] = $now['year']; | |
c5670e9e | 1203 | unset($from); |
1204 | break; | |
1205 | ||
1206 | case 'next': | |
1207 | $from['M'] = $from['d'] = 1; | |
353ffa53 TO |
1208 | $to['d'] = 31; |
1209 | $to['M'] = 12; | |
1210 | $to['Y'] = $from['Y'] = $now['year'] + 1; | |
c5670e9e | 1211 | break; |
1212 | ||
1213 | case 'starting': | |
1214 | $from['d'] = $now['mday']; | |
1215 | $from['M'] = $now['mon']; | |
1216 | $from['Y'] = $now['year']; | |
1217 | $to['d'] = $now['mday'] - 1; | |
1218 | $to['M'] = $now['mon']; | |
1219 | $to['Y'] = $now['year'] + 1; | |
1220 | break; | |
ad336d61 | 1221 | |
1222 | default: | |
e34c9274 | 1223 | switch ($relativeTermPrefix) { |
1224 | ||
1225 | case 'ending': | |
1226 | $to['d'] = $now['mday']; | |
1227 | $to['M'] = $now['mon']; | |
1228 | $to['Y'] = $now['year']; | |
1229 | $to['H'] = 23; | |
1230 | $to['i'] = $to['s'] = 59; | |
1231 | $from = self::intervalAdd('year', -$relativeTermSuffix, $to); | |
1232 | $from = self::intervalAdd('second', 1, $from); | |
1233 | break; | |
1234 | ||
1235 | case 'this': | |
1236 | $from['d'] = $from['M'] = 1; | |
1237 | $to['d'] = 31; | |
1238 | $to['M'] = 12; | |
1239 | $to['Y'] = $from['Y'] = $now['year']; | |
1240 | if (is_numeric($relativeTermSuffix)) { | |
1241 | $from['Y'] -= ($relativeTermSuffix - 1); | |
1242 | } | |
1243 | break; | |
ad336d61 | 1244 | } |
e34c9274 | 1245 | break; |
6a488035 TO |
1246 | } |
1247 | break; | |
1248 | ||
1249 | case 'fiscal_year': | |
353ffa53 | 1250 | $config = CRM_Core_Config::singleton(); |
6a488035 TO |
1251 | $from['d'] = $config->fiscalYearStart['d']; |
1252 | $from['M'] = $config->fiscalYearStart['M']; | |
353ffa53 | 1253 | $fYear = self::calculateFiscalYear($from['d'], $from['M']); |
1e60a380 | 1254 | switch ($relativeTermPrefix) { |
6a488035 | 1255 | case 'this': |
353ffa53 | 1256 | $from['Y'] = $fYear; |
5eb3092f | 1257 | $fiscalYear = mktime(0, 0, 0, $from['M'], $from['d'] - 1, $from['Y'] + 1); |
353ffa53 | 1258 | $fiscalEnd = explode('-', date("Y-m-d", $fiscalYear)); |
6a488035 TO |
1259 | $to['d'] = $fiscalEnd['2']; |
1260 | $to['M'] = $fiscalEnd['1']; | |
1261 | $to['Y'] = $fiscalEnd['0']; | |
e34c9274 | 1262 | $to['H'] = 23; |
1263 | $to['i'] = $to['s'] = 59; | |
1264 | if (is_numeric($relativeTermSuffix)) { | |
1265 | $from = self::intervalAdd('year', (-$relativeTermSuffix), $to); | |
1266 | $from = self::intervalAdd('second', 1, $from); | |
1267 | } | |
6a488035 TO |
1268 | break; |
1269 | ||
1270 | case 'previous': | |
1e60a380 | 1271 | if (!is_numeric($relativeTermSuffix)) { |
1272 | $from['Y'] = ($relativeTermSuffix === 'before') ? $fYear - 2 : $fYear - 1; | |
1273 | $fiscalYear = mktime(0, 0, 0, $from['M'], $from['d'] - 1, $from['Y'] + 1); | |
1274 | $fiscalEnd = explode('-', date("Y-m-d", $fiscalYear)); | |
1275 | $to['d'] = $fiscalEnd['2']; | |
1276 | $to['M'] = $fiscalEnd['1']; | |
1277 | $to['Y'] = $fiscalEnd['0']; | |
e34c9274 | 1278 | $to['H'] = 23; |
1279 | $to['i'] = $to['s'] = 59; | |
1e60a380 | 1280 | } |
1281 | else { | |
1282 | $from['Y'] = $fYear - $relativeTermSuffix; | |
1283 | $fiscalYear = mktime(0, 0, 0, $from['M'], $from['d'] - 1, $from['Y'] + 1); | |
1284 | $fiscalEnd = explode('-', date("Y-m-d", $fiscalYear)); | |
1285 | $to['d'] = $fiscalEnd['2']; | |
1286 | $to['M'] = $fiscalEnd['1']; | |
1287 | $to['Y'] = $fYear; | |
e34c9274 | 1288 | $to['H'] = 23; |
1289 | $to['i'] = $to['s'] = 59; | |
1e60a380 | 1290 | } |
c5670e9e | 1291 | break; |
6a488035 | 1292 | |
c5670e9e | 1293 | case 'next': |
353ffa53 | 1294 | $from['Y'] = $fYear + 1; |
5eb3092f | 1295 | $fiscalYear = mktime(0, 0, 0, $from['M'], $from['d'] - 1, $from['Y'] + 1); |
353ffa53 | 1296 | $fiscalEnd = explode('-', date("Y-m-d", $fiscalYear)); |
6a488035 TO |
1297 | $to['d'] = $fiscalEnd['2']; |
1298 | $to['M'] = $fiscalEnd['1']; | |
1299 | $to['Y'] = $fiscalEnd['0']; | |
1300 | break; | |
1301 | } | |
1302 | break; | |
1303 | ||
1304 | case 'quarter': | |
1305 | switch ($relativeTerm) { | |
1306 | case 'this': | |
1307 | ||
353ffa53 | 1308 | $quarter = ceil($now['mon'] / 3); |
6a488035 TO |
1309 | $from['d'] = 1; |
1310 | $from['M'] = (3 * $quarter) - 2; | |
353ffa53 TO |
1311 | $to['M'] = 3 * $quarter; |
1312 | $to['Y'] = $from['Y'] = $now['year']; | |
1313 | $to['d'] = date('t', mktime(0, 0, 0, $to['M'], 1, $now['year'])); | |
6a488035 TO |
1314 | break; |
1315 | ||
1316 | case 'previous': | |
353ffa53 TO |
1317 | $difference = 1; |
1318 | $quarter = ceil($now['mon'] / 3); | |
1319 | $quarter = $quarter - $difference; | |
6a488035 TO |
1320 | $subtractYear = 0; |
1321 | if ($quarter <= 0) { | |
1322 | $subtractYear = 1; | |
1323 | $quarter += 4; | |
1324 | } | |
1325 | $from['d'] = 1; | |
1326 | $from['M'] = (3 * $quarter) - 2; | |
353ffa53 TO |
1327 | $to['M'] = 3 * $quarter; |
1328 | $to['Y'] = $from['Y'] = $now['year'] - $subtractYear; | |
1329 | $to['d'] = date('t', mktime(0, 0, 0, $to['M'], 1, $to['Y'])); | |
6a488035 TO |
1330 | break; |
1331 | ||
1332 | case 'previous_before': | |
1333 | $difference = 2; | |
353ffa53 TO |
1334 | $quarter = ceil($now['mon'] / 3); |
1335 | $quarter = $quarter - $difference; | |
d75f2f47 | 1336 | $subtractYear = 0; |
6a488035 TO |
1337 | if ($quarter <= 0) { |
1338 | $subtractYear = 1; | |
1339 | $quarter += 4; | |
1340 | } | |
1341 | $from['d'] = 1; | |
1342 | $from['M'] = (3 * $quarter) - 2; | |
353ffa53 TO |
1343 | $to['M'] = 3 * $quarter; |
1344 | $to['Y'] = $from['Y'] = $now['year'] - $subtractYear; | |
1345 | $to['d'] = date('t', mktime(0, 0, 0, $to['M'], 1, $to['Y'])); | |
6a488035 TO |
1346 | break; |
1347 | ||
1348 | case 'previous_2': | |
353ffa53 TO |
1349 | $difference = 2; |
1350 | $quarter = ceil($now['mon'] / 3); | |
6a488035 | 1351 | $current_quarter = $quarter; |
353ffa53 TO |
1352 | $quarter = $quarter - $difference; |
1353 | $subtractYear = 0; | |
6a488035 TO |
1354 | if ($quarter <= 0) { |
1355 | $subtractYear = 1; | |
1356 | $quarter += 4; | |
1357 | } | |
1358 | $from['d'] = 1; | |
1359 | $from['M'] = (3 * $quarter) - 2; | |
1360 | switch ($current_quarter) { | |
1361 | case 1: | |
1362 | $to['M'] = (4 * $quarter); | |
1363 | break; | |
1364 | ||
1365 | case 2: | |
1366 | $to['M'] = (4 * $quarter) + 3; | |
1367 | break; | |
1368 | ||
1369 | case 3: | |
1370 | $to['M'] = (4 * $quarter) + 2; | |
1371 | break; | |
1372 | ||
1373 | case 4: | |
1374 | $to['M'] = (4 * $quarter) + 1; | |
1375 | break; | |
1376 | } | |
1377 | $to['Y'] = $from['Y'] = $now['year'] - $subtractYear; | |
1378 | if ($to['M'] > 12) { | |
1379 | $to['M'] = 3 * ($quarter - 3); | |
1380 | $to['Y'] = $now['year']; | |
1381 | } | |
1382 | $to['d'] = date('t', mktime(0, 0, 0, $to['M'], 1, $to['Y'])); | |
1383 | break; | |
1384 | ||
1385 | case 'earlier': | |
1386 | $quarter = ceil($now['mon'] / 3) - 1; | |
d75f2f47 | 1387 | $subtractYear = 0; |
6a488035 TO |
1388 | if ($quarter <= 0) { |
1389 | $subtractYear = 1; | |
1390 | $quarter += 4; | |
1391 | } | |
1392 | $to['M'] = 3 * $quarter; | |
1393 | $to['Y'] = $from['Y'] = $now['year'] - $subtractYear; | |
1394 | $to['d'] = date('t', mktime(0, 0, 0, $to['M'], 1, $to['Y'])); | |
1395 | unset($from); | |
1396 | break; | |
1397 | ||
1398 | case 'greater': | |
353ffa53 | 1399 | $quarter = ceil($now['mon'] / 3); |
6a488035 TO |
1400 | $from['d'] = 1; |
1401 | $from['M'] = (3 * $quarter) - 2; | |
1402 | $from['Y'] = $now['year']; | |
1403 | unset($to); | |
1404 | break; | |
d75f2f47 | 1405 | |
52f15bd6 | 1406 | case 'greater_previous': |
1407 | $quarter = ceil($now['mon'] / 3) - 1; | |
353ffa53 | 1408 | $subtractYear = 0; |
52f15bd6 | 1409 | if ($quarter <= 0) { |
1410 | $subtractYear = 1; | |
1411 | $quarter += 4; | |
1412 | } | |
1413 | $from['M'] = 3 * $quarter; | |
1414 | $from['Y'] = $from['Y'] = $now['year'] - $subtractYear; | |
1415 | $from['d'] = date('t', mktime(0, 0, 0, $from['M'], 1, $from['Y'])); | |
1416 | unset($to); | |
1417 | break; | |
6a488035 TO |
1418 | |
1419 | case 'ending': | |
1420 | $to['d'] = $now['mday']; | |
1421 | $to['M'] = $now['mon']; | |
1422 | $to['Y'] = $now['year']; | |
1423 | $to['H'] = 23; | |
1424 | $to['i'] = $to['s'] = 59; | |
2de679ba | 1425 | $from = self::intervalAdd('day', -90, $to); |
353ffa53 | 1426 | $from = self::intervalAdd('second', 1, $from); |
6a488035 | 1427 | break; |
e902edd1 | 1428 | |
1429 | case 'current': | |
353ffa53 | 1430 | $quarter = ceil($now['mon'] / 3); |
e902edd1 | 1431 | $from['d'] = 1; |
1432 | $from['M'] = (3 * $quarter) - 2; | |
1433 | $from['Y'] = $now['year']; | |
1434 | $to['d'] = $now['mday']; | |
1435 | $to['M'] = $now['mon']; | |
1436 | $to['Y'] = $now['year']; | |
1437 | $to['H'] = 23; | |
1438 | $to['i'] = $to['s'] = 59; | |
1439 | break; | |
c5670e9e | 1440 | |
1441 | case 'less': | |
353ffa53 TO |
1442 | $quarter = ceil($now['mon'] / 3); |
1443 | $to['M'] = 3 * $quarter; | |
1444 | $to['Y'] = $now['year']; | |
1445 | $to['d'] = date('t', mktime(0, 0, 0, $to['M'], 1, $now['year'])); | |
c5670e9e | 1446 | unset($from); |
1447 | break; | |
1448 | ||
1449 | case 'next': | |
1450 | $difference = -1; | |
1451 | $subtractYear = 0; | |
353ffa53 TO |
1452 | $quarter = ceil($now['mon'] / 3); |
1453 | $quarter = $quarter - $difference; | |
50bfb460 | 1454 | // CRM-14550 QA Fix |
22e263ad | 1455 | if ($quarter > 4) { |
c5670e9e | 1456 | $now['year'] = $now['year'] + 1; |
1457 | $quarter = 1; | |
1458 | } | |
1459 | if ($quarter <= 0) { | |
1460 | $subtractYear = 1; | |
1461 | $quarter += 4; | |
1462 | } | |
1463 | $from['d'] = 1; | |
1464 | $from['M'] = (3 * $quarter) - 2; | |
353ffa53 TO |
1465 | $to['M'] = 3 * $quarter; |
1466 | $to['Y'] = $from['Y'] = $now['year'] - $subtractYear; | |
1467 | $to['d'] = date('t', mktime(0, 0, 0, $to['M'], 1, $to['Y'])); | |
c5670e9e | 1468 | break; |
2de679ba | 1469 | |
1470 | case 'starting': | |
1471 | $from['d'] = $now['mday']; | |
1472 | $from['M'] = $now['mon']; | |
1473 | $from['Y'] = $now['year']; | |
1474 | $from['H'] = 00; | |
1475 | $from['i'] = $to['s'] = 00; | |
1476 | $to = self::intervalAdd('day', 90, $from); | |
1477 | $to = self::intervalAdd('second', -1, $to); | |
1478 | break; | |
ad336d61 | 1479 | |
1480 | default: | |
1481 | if ($relativeTermPrefix === 'ending') { | |
1482 | $to['d'] = $now['mday']; | |
1483 | $to['M'] = $now['mon']; | |
1484 | $to['Y'] = $now['year']; | |
1485 | $to['H'] = 23; | |
1486 | $to['i'] = $to['s'] = 59; | |
1487 | $from = self::intervalAdd('month', -($relativeTermSuffix * 3), $to); | |
1488 | $from = self::intervalAdd('second', 1, $from); | |
1489 | } | |
6a488035 TO |
1490 | } |
1491 | break; | |
1492 | ||
1493 | case 'month': | |
1494 | switch ($relativeTerm) { | |
1495 | case 'this': | |
1496 | $from['d'] = 1; | |
353ffa53 | 1497 | $to['d'] = date('t', mktime(0, 0, 0, $now['mon'], 1, $now['year'])); |
6a488035 TO |
1498 | $from['M'] = $to['M'] = $now['mon']; |
1499 | $from['Y'] = $to['Y'] = $now['year']; | |
1500 | break; | |
1501 | ||
1502 | case 'previous': | |
1503 | $from['d'] = 1; | |
1504 | if ($now['mon'] == 1) { | |
1505 | $from['M'] = $to['M'] = 12; | |
1506 | $from['Y'] = $to['Y'] = $now['year'] - 1; | |
1507 | } | |
1508 | else { | |
1509 | $from['M'] = $to['M'] = $now['mon'] - 1; | |
1510 | $from['Y'] = $to['Y'] = $now['year']; | |
1511 | } | |
1512 | $to['d'] = date('t', mktime(0, 0, 0, $to['M'], 1, $to['Y'])); | |
1513 | break; | |
1514 | ||
1515 | case 'previous_before': | |
1516 | $from['d'] = 1; | |
1517 | if ($now['mon'] < 3) { | |
1518 | $from['M'] = $to['M'] = 10 + $now['mon']; | |
1519 | $from['Y'] = $to['Y'] = $now['year'] - 1; | |
1520 | } | |
1521 | else { | |
1522 | $from['M'] = $to['M'] = $now['mon'] - 2; | |
1523 | $from['Y'] = $to['Y'] = $now['year']; | |
1524 | } | |
1525 | $to['d'] = date('t', mktime(0, 0, 0, $to['M'], 1, $to['Y'])); | |
1526 | break; | |
1527 | ||
1528 | case 'previous_2': | |
1529 | $from['d'] = 1; | |
1530 | if ($now['mon'] < 3) { | |
1531 | $from['M'] = 10 + $now['mon']; | |
1532 | $from['Y'] = $now['year'] - 1; | |
1533 | } | |
1534 | else { | |
1535 | $from['M'] = $now['mon'] - 2; | |
1536 | $from['Y'] = $now['year']; | |
1537 | } | |
1538 | ||
1539 | if ($now['mon'] == 1) { | |
1540 | $to['M'] = 12; | |
1541 | $to['Y'] = $now['year'] - 1; | |
1542 | } | |
1543 | else { | |
1544 | $to['M'] = $now['mon'] - 1; | |
1545 | $to['Y'] = $now['year']; | |
1546 | } | |
1547 | ||
1548 | $to['d'] = date('t', mktime(0, 0, 0, $to['M'], 1, $to['Y'])); | |
1549 | break; | |
1550 | ||
1551 | case 'earlier': | |
50bfb460 | 1552 | // before end of past month |
6a488035 TO |
1553 | if ($now['mon'] == 1) { |
1554 | $to['M'] = 12; | |
1555 | $to['Y'] = $now['year'] - 1; | |
1556 | } | |
1557 | else { | |
1558 | $to['M'] = $now['mon'] - 1; | |
1559 | $to['Y'] = $now['year']; | |
1560 | } | |
1561 | ||
1562 | $to['d'] = date('t', mktime(0, 0, 0, $to['M'], 1, $to['Y'])); | |
1563 | unset($from); | |
1564 | break; | |
1565 | ||
1566 | case 'greater': | |
1567 | $from['d'] = 1; | |
fe7f4414 | 1568 | $from['M'] = $now['mon']; |
6a488035 TO |
1569 | $from['Y'] = $now['year']; |
1570 | unset($to); | |
1571 | break; | |
1572 | ||
52f15bd6 | 1573 | case 'greater_previous': |
50bfb460 | 1574 | // from end of past month |
52f15bd6 | 1575 | if ($now['mon'] == 1) { |
1576 | $from['M'] = 12; | |
1577 | $from['Y'] = $now['year'] - 1; | |
1578 | } | |
1579 | else { | |
1580 | $from['M'] = $now['mon'] - 1; | |
1581 | $from['Y'] = $now['year']; | |
1582 | } | |
1583 | ||
1584 | $from['d'] = date('t', mktime(0, 0, 0, $from['M'], 1, $from['Y'])); | |
1585 | unset($to); | |
1586 | break; | |
1587 | ||
0db07823 | 1588 | case 'ending_2': |
1589 | $to['d'] = $now['mday']; | |
1590 | $to['M'] = $now['mon']; | |
1591 | $to['Y'] = $now['year']; | |
1592 | $to['H'] = 23; | |
1593 | $to['i'] = $to['s'] = 59; | |
1594 | $from = self::intervalAdd('day', -60, $to); | |
1595 | $from = self::intervalAdd('second', 1, $from); | |
1596 | break; | |
1597 | ||
6a488035 TO |
1598 | case 'ending': |
1599 | $to['d'] = $now['mday']; | |
1600 | $to['M'] = $now['mon']; | |
1601 | $to['Y'] = $now['year']; | |
1602 | $to['H'] = 23; | |
1603 | $to['i'] = $to['s'] = 59; | |
315eef7b | 1604 | $from = self::intervalAdd('day', -30, $to); |
353ffa53 | 1605 | $from = self::intervalAdd('second', 1, $from); |
6a488035 | 1606 | break; |
e902edd1 | 1607 | |
1608 | case 'current': | |
1609 | $from['d'] = 1; | |
fe7f4414 | 1610 | $from['M'] = $now['mon']; |
e902edd1 | 1611 | $from['Y'] = $now['year']; |
1612 | $to['d'] = $now['mday']; | |
1613 | $to['M'] = $now['mon']; | |
1614 | $to['Y'] = $now['year']; | |
1615 | $to['H'] = 23; | |
1616 | $to['i'] = $to['s'] = 59; | |
1617 | break; | |
c5670e9e | 1618 | |
1619 | case 'less': | |
50bfb460 | 1620 | // CRM-14550 QA Fix |
c5670e9e | 1621 | $to['Y'] = $now['year']; |
1622 | $to['M'] = $now['mon']; | |
1623 | $to['d'] = date('t', mktime(0, 0, 0, $now['mon'], 1, $now['year'])); | |
1624 | unset($from); | |
1625 | break; | |
1626 | ||
1627 | case 'next': | |
1628 | $from['d'] = 1; | |
1629 | if ($now['mon'] == 12) { | |
1630 | $from['M'] = $to['M'] = 1; | |
1631 | $from['Y'] = $to['Y'] = $now['year'] + 1; | |
1632 | } | |
1633 | else { | |
1634 | $from['M'] = $to['M'] = $now['mon'] + 1; | |
1635 | $from['Y'] = $to['Y'] = $now['year']; | |
1636 | } | |
1637 | $to['d'] = date('t', mktime(0, 0, 0, $to['M'], 1, $to['Y'])); | |
1638 | break; | |
1639 | ||
1640 | case 'starting': | |
2db7c428 | 1641 | $from['d'] = $now['mday']; |
1642 | $from['M'] = $now['mon']; | |
1643 | $from['Y'] = $now['year']; | |
1644 | $from['H'] = 00; | |
1645 | $from['i'] = $to['s'] = 00; | |
1646 | $to = self::intervalAdd('day', 30, $from); | |
0db07823 | 1647 | $to = self::intervalAdd('second', -1, $to); |
1648 | break; | |
1649 | ||
1650 | case 'starting_2': | |
1651 | $from['d'] = $now['mday']; | |
1652 | $from['M'] = $now['mon']; | |
1653 | $from['Y'] = $now['year']; | |
1654 | $from['H'] = 00; | |
1655 | $from['i'] = $to['s'] = 00; | |
1656 | $to = self::intervalAdd('day', 60, $from); | |
2db7c428 | 1657 | $to = self::intervalAdd('second', -1, $to); |
c5670e9e | 1658 | break; |
ad336d61 | 1659 | |
1660 | default: | |
1661 | if ($relativeTermPrefix === 'ending') { | |
1662 | $to['d'] = $now['mday']; | |
1663 | $to['M'] = $now['mon']; | |
1664 | $to['Y'] = $now['year']; | |
1665 | $to['H'] = 23; | |
1666 | $to['i'] = $to['s'] = 59; | |
1667 | $from = self::intervalAdd($unit, -$relativeTermSuffix, $to); | |
1668 | $from = self::intervalAdd('second', 1, $from); | |
1669 | } | |
6a488035 TO |
1670 | } |
1671 | break; | |
1672 | ||
1673 | case 'week': | |
aaffa79f | 1674 | $weekFirst = Civi::settings()->get('weekBegins'); |
a3d92acb | 1675 | $thisDay = $now['wday']; |
38d131e3 | 1676 | if ($weekFirst > $thisDay) { |
a3d92acb | 1677 | $diffDay = $thisDay - $weekFirst + 7; |
1678 | } | |
1679 | else { | |
1680 | $diffDay = $thisDay - $weekFirst; | |
1681 | } | |
6a488035 TO |
1682 | switch ($relativeTerm) { |
1683 | case 'this': | |
1684 | $from['d'] = $now['mday']; | |
1685 | $from['M'] = $now['mon']; | |
1686 | $from['Y'] = $now['year']; | |
a3d92acb | 1687 | $from = self::intervalAdd('day', -1 * ($diffDay), $from); |
353ffa53 | 1688 | $to = self::intervalAdd('day', 6, $from); |
6a488035 TO |
1689 | break; |
1690 | ||
1691 | case 'previous': | |
1692 | $from['d'] = $now['mday']; | |
1693 | $from['M'] = $now['mon']; | |
1694 | $from['Y'] = $now['year']; | |
a3d92acb | 1695 | $from = self::intervalAdd('day', -1 * ($diffDay) - 7, $from); |
353ffa53 | 1696 | $to = self::intervalAdd('day', 6, $from); |
6a488035 TO |
1697 | break; |
1698 | ||
1699 | case 'previous_before': | |
1700 | $from['d'] = $now['mday']; | |
1701 | $from['M'] = $now['mon']; | |
1702 | $from['Y'] = $now['year']; | |
a3d92acb | 1703 | $from = self::intervalAdd('day', -1 * ($diffDay) - 14, $from); |
353ffa53 | 1704 | $to = self::intervalAdd('day', 6, $from); |
6a488035 TO |
1705 | break; |
1706 | ||
1707 | case 'previous_2': | |
1708 | $from['d'] = $now['mday']; | |
1709 | $from['M'] = $now['mon']; | |
1710 | $from['Y'] = $now['year']; | |
a3d92acb | 1711 | $from = self::intervalAdd('day', -1 * ($diffDay) - 14, $from); |
353ffa53 | 1712 | $to = self::intervalAdd('day', 13, $from); |
6a488035 TO |
1713 | break; |
1714 | ||
1715 | case 'earlier': | |
1716 | $to['d'] = $now['mday']; | |
1717 | $to['M'] = $now['mon']; | |
1718 | $to['Y'] = $now['year']; | |
a3d92acb | 1719 | $to = self::intervalAdd('day', -1 * ($diffDay) - 1, $to); |
6a488035 TO |
1720 | unset($from); |
1721 | break; | |
1722 | ||
1723 | case 'greater': | |
1724 | $from['d'] = $now['mday']; | |
1725 | $from['M'] = $now['mon']; | |
1726 | $from['Y'] = $now['year']; | |
a3d92acb | 1727 | $from = self::intervalAdd('day', -1 * ($diffDay), $from); |
6a488035 TO |
1728 | unset($to); |
1729 | break; | |
1730 | ||
52f15bd6 | 1731 | case 'greater_previous': |
1732 | $from['d'] = $now['mday']; | |
1733 | $from['M'] = $now['mon']; | |
1734 | $from['Y'] = $now['year']; | |
a3d92acb | 1735 | $from = self::intervalAdd('day', -1 * ($diffDay) - 1, $from); |
52f15bd6 | 1736 | unset($to); |
1737 | break; | |
1738 | ||
6a488035 TO |
1739 | case 'ending': |
1740 | $to['d'] = $now['mday']; | |
1741 | $to['M'] = $now['mon']; | |
1742 | $to['Y'] = $now['year']; | |
1743 | $to['H'] = 23; | |
1744 | $to['i'] = $to['s'] = 59; | |
353ffa53 TO |
1745 | $from = self::intervalAdd('day', -7, $to); |
1746 | $from = self::intervalAdd('second', 1, $from); | |
6a488035 | 1747 | break; |
e902edd1 | 1748 | |
1749 | case 'current': | |
1750 | $from['d'] = $now['mday']; | |
1751 | $from['M'] = $now['mon']; | |
1752 | $from['Y'] = $now['year']; | |
a3d92acb | 1753 | $from = self::intervalAdd('day', -1 * ($diffDay), $from); |
e902edd1 | 1754 | $to['d'] = $now['mday']; |
1755 | $to['M'] = $now['mon']; | |
1756 | $to['Y'] = $now['year']; | |
1757 | $to['H'] = 23; | |
1758 | $to['i'] = $to['s'] = 59; | |
1759 | break; | |
c5670e9e | 1760 | |
1761 | case 'less': | |
1762 | $to['d'] = $now['mday']; | |
1763 | $to['M'] = $now['mon']; | |
1764 | $to['Y'] = $now['year']; | |
50bfb460 | 1765 | // CRM-14550 QA Fix |
a3d92acb | 1766 | $to = self::intervalAdd('day', -1 * ($diffDay) + 6, $to); |
c5670e9e | 1767 | unset($from); |
1768 | break; | |
1769 | ||
1770 | case 'next': | |
1771 | $from['d'] = $now['mday']; | |
1772 | $from['M'] = $now['mon']; | |
1773 | $from['Y'] = $now['year']; | |
a3d92acb | 1774 | $from = self::intervalAdd('day', -1 * ($diffDay) + 7, $from); |
d5cc0fc2 | 1775 | $to = self::intervalAdd('day', 6, $from); |
c5670e9e | 1776 | break; |
1777 | ||
1778 | case 'starting': | |
1779 | $from['d'] = $now['mday']; | |
1780 | $from['M'] = $now['mon']; | |
1781 | $from['Y'] = $now['year']; | |
1782 | $from['H'] = 00; | |
1783 | $from['i'] = $to['s'] = 00; | |
d5cc0fc2 | 1784 | $to = self::intervalAdd('day', 7, $from); |
353ffa53 | 1785 | $to = self::intervalAdd('second', -1, $to); |
c5670e9e | 1786 | break; |
ad336d61 | 1787 | |
1788 | default: | |
1789 | if ($relativeTermPrefix === 'ending') { | |
1790 | $to['d'] = $now['mday']; | |
1791 | $to['M'] = $now['mon']; | |
1792 | $to['Y'] = $now['year']; | |
1793 | $to['H'] = 23; | |
1794 | $to['i'] = $to['s'] = 59; | |
1795 | $from = self::intervalAdd($unit, -$relativeTermSuffix, $to); | |
1796 | $from = self::intervalAdd('second', 1, $from); | |
1797 | } | |
6a488035 TO |
1798 | } |
1799 | break; | |
1800 | ||
1801 | case 'day': | |
1802 | switch ($relativeTerm) { | |
1803 | case 'this': | |
1804 | $from['d'] = $to['d'] = $now['mday']; | |
1805 | $from['M'] = $to['M'] = $now['mon']; | |
1806 | $from['Y'] = $to['Y'] = $now['year']; | |
1807 | break; | |
1808 | ||
1809 | case 'previous': | |
1810 | $from['d'] = $now['mday']; | |
1811 | $from['M'] = $now['mon']; | |
1812 | $from['Y'] = $now['year']; | |
353ffa53 TO |
1813 | $from = self::intervalAdd('day', -1, $from); |
1814 | $to['d'] = $from['d']; | |
1815 | $to['M'] = $from['M']; | |
1816 | $to['Y'] = $from['Y']; | |
6a488035 TO |
1817 | break; |
1818 | ||
1819 | case 'previous_before': | |
1820 | $from['d'] = $now['mday']; | |
1821 | $from['M'] = $now['mon']; | |
1822 | $from['Y'] = $now['year']; | |
353ffa53 TO |
1823 | $from = self::intervalAdd('day', -2, $from); |
1824 | $to['d'] = $from['d']; | |
1825 | $to['M'] = $from['M']; | |
1826 | $to['Y'] = $from['Y']; | |
6a488035 TO |
1827 | break; |
1828 | ||
1829 | case 'previous_2': | |
1830 | $from['d'] = $to['d'] = $now['mday']; | |
1831 | $from['M'] = $to['M'] = $now['mon']; | |
1832 | $from['Y'] = $to['Y'] = $now['year']; | |
353ffa53 TO |
1833 | $from = self::intervalAdd('day', -2, $from); |
1834 | $to = self::intervalAdd('day', -1, $to); | |
6a488035 TO |
1835 | break; |
1836 | ||
1837 | case 'earlier': | |
1838 | $to['d'] = $now['mday']; | |
1839 | $to['M'] = $now['mon']; | |
1840 | $to['Y'] = $now['year']; | |
36f71190 | 1841 | $to = self::intervalAdd('day', -1, $to); |
6a488035 TO |
1842 | unset($from); |
1843 | break; | |
1844 | ||
1845 | case 'greater': | |
1846 | $from['d'] = $now['mday']; | |
fe7f4414 | 1847 | $from['M'] = $now['mon']; |
6a488035 TO |
1848 | $from['Y'] = $now['year']; |
1849 | unset($to); | |
1850 | break; | |
c5670e9e | 1851 | |
1852 | case 'starting': | |
353ffa53 TO |
1853 | $to['d'] = $now['mday']; |
1854 | $to['M'] = $now['mon']; | |
1855 | $to['Y'] = $now['year']; | |
d5cc0fc2 | 1856 | $to = self::intervalAdd('day', 1, $to); |
c5670e9e | 1857 | $from['d'] = $to['d']; |
1858 | $from['M'] = $to['M']; | |
1859 | $from['Y'] = $to['Y']; | |
1860 | break; | |
1861 | ||
ad336d61 | 1862 | default: |
1863 | if ($relativeTermPrefix === 'ending') { | |
1864 | $to['d'] = $now['mday']; | |
1865 | $to['M'] = $now['mon']; | |
1866 | $to['Y'] = $now['year']; | |
1867 | $to['H'] = 23; | |
1868 | $to['i'] = $to['s'] = 59; | |
1869 | $from = self::intervalAdd($unit, -$relativeTermSuffix, $to); | |
1870 | $from = self::intervalAdd('second', 1, $from); | |
1871 | } | |
6a488035 TO |
1872 | } |
1873 | break; | |
1874 | } | |
1875 | ||
e34c9274 | 1876 | $dateRange['from'] = empty($from) ? NULL : self::format($from); |
1877 | $dateRange['to'] = empty($to) ? NULL : self::format($to); | |
6a488035 TO |
1878 | return $dateRange; |
1879 | } | |
1880 | ||
1881 | /** | |
fe482240 | 1882 | * Calculate current fiscal year based on the fiscal month and day. |
6a488035 | 1883 | * |
77855840 TO |
1884 | * @param int $fyDate |
1885 | * Fiscal start date. | |
6a488035 | 1886 | * |
77855840 TO |
1887 | * @param int $fyMonth |
1888 | * Fiscal Start Month. | |
6a488035 | 1889 | * |
a6c01b45 | 1890 | * @return int |
1e60a380 | 1891 | * $fy Current Fiscal Year |
6a488035 | 1892 | */ |
00be9182 | 1893 | public static function calculateFiscalYear($fyDate, $fyMonth) { |
6a488035 TO |
1894 | $date = date("Y-m-d"); |
1895 | $currentYear = date("Y"); | |
1896 | ||
50bfb460 | 1897 | // recalculate the date because month 4::04 make the difference |
353ffa53 TO |
1898 | $fiscalYear = explode('-', date("Y-m-d", mktime(0, 0, 0, $fyMonth, $fyDate, $currentYear))); |
1899 | $fyDate = $fiscalYear[2]; | |
1900 | $fyMonth = $fiscalYear[1]; | |
6a488035 TO |
1901 | $fyStartDate = date("Y-m-d", mktime(0, 0, 0, $fyMonth, $fyDate, $currentYear)); |
1902 | ||
1903 | if ($fyStartDate > $date) { | |
1904 | $fy = intval(intval($currentYear) - 1); | |
1905 | } | |
1906 | else { | |
1907 | $fy = intval($currentYear); | |
1908 | } | |
1909 | return $fy; | |
1910 | } | |
1911 | ||
1912 | /** | |
50bfb460 | 1913 | * Function to process date, convert to mysql format |
6a488035 | 1914 | * |
77855840 TO |
1915 | * @param string $date |
1916 | * Date string. | |
1917 | * @param string $time | |
1918 | * Time string. | |
f4aaa82a | 1919 | * @param bool|string $returnNullString 'null' needs to be returned |
6a488035 | 1920 | * so that db oject will set null in db |
77855840 TO |
1921 | * @param string $format |
1922 | * Expected return date format.( default is mysql ). | |
6a488035 | 1923 | * |
a6c01b45 CW |
1924 | * @return string |
1925 | * date format that is excepted by mysql | |
6a488035 | 1926 | */ |
00be9182 | 1927 | public static function processDate($date, $time = NULL, $returnNullString = FALSE, $format = 'YmdHis') { |
6a488035 TO |
1928 | $mysqlDate = NULL; |
1929 | ||
1930 | if ($returnNullString) { | |
1931 | $mysqlDate = 'null'; | |
1932 | } | |
1933 | ||
1934 | if (trim($date)) { | |
1935 | $mysqlDate = date($format, strtotime($date . ' ' . $time)); | |
1936 | } | |
1937 | ||
1938 | return $mysqlDate; | |
1939 | } | |
1940 | ||
d86e674f | 1941 | /** |
1942 | * Add the metadata about a date field to the field. | |
1943 | * | |
1944 | * This metadata will work with the call $form->add('datepicker', ... | |
1945 | * | |
1946 | * @param array $fieldMetaData | |
1947 | * @param array $field | |
1948 | * | |
1949 | * @return array | |
1950 | */ | |
1951 | public static function addDateMetadataToField($fieldMetaData, $field) { | |
1952 | if (isset($fieldMetaData['html'])) { | |
1953 | $field['html_type'] = $fieldMetaData['html']['type']; | |
e9f229db | 1954 | if ($field['html_type'] === 'Select Date') { |
1955 | if (!isset($field['date_format'])) { | |
1956 | $dateAttributes = CRM_Core_SelectValues::date($fieldMetaData['html']['formatType'], NULL, NULL, NULL, 'Input'); | |
1957 | $field['start_date_years'] = $dateAttributes['minYear']; | |
1958 | $field['end_date_years'] = $dateAttributes['maxYear']; | |
1959 | $field['date_format'] = $dateAttributes['format']; | |
1960 | $field['is_datetime_field'] = TRUE; | |
1961 | $field['time_format'] = $dateAttributes['time']; | |
ed0ca248 | 1962 | $field['smarty_view_format'] = $dateAttributes['smarty_view_format']; |
d86e674f | 1963 | } |
e9f229db | 1964 | $field['datepicker']['extra'] = self::getDatePickerExtra($field); |
1965 | $field['datepicker']['attributes'] = self::getDatePickerAttributes($field); | |
d86e674f | 1966 | } |
d86e674f | 1967 | } |
1968 | return $field; | |
1969 | } | |
1970 | ||
d86e674f | 1971 | /** |
1972 | * Get the fields required for the 'extra' parameter when adding a datepicker. | |
1973 | * | |
1974 | * @param array $field | |
1975 | * | |
1976 | * @return array | |
1977 | */ | |
1978 | public static function getDatePickerExtra($field) { | |
be2fb01f | 1979 | $extra = []; |
d86e674f | 1980 | if (isset($field['date_format'])) { |
1981 | $extra['date'] = $field['date_format']; | |
1982 | $extra['time'] = $field['time_format']; | |
1983 | } | |
1984 | $thisYear = date('Y'); | |
1985 | if (isset($field['start_date_years'])) { | |
1986 | $extra['minDate'] = date('Y-m-d', strtotime('-' . ($thisYear - $field['start_date_years']) . ' years')); | |
1987 | } | |
1988 | if (isset($field['end_date_years'])) { | |
1989 | $extra['maxDate'] = date('Y-m-d', strtotime('-' . ($thisYear - $field['end_date_years']) . ' years')); | |
1990 | } | |
1991 | return $extra; | |
1992 | } | |
1993 | ||
1994 | /** | |
1995 | * Get the attributes parameters required for datepicker. | |
1996 | * | |
1997 | * @param array $field | |
1998 | * Field metadata | |
1999 | * | |
2000 | * @return array | |
2001 | * Array ready to pass to $this->addForm('datepicker' as attributes. | |
2002 | */ | |
2003 | public static function getDatePickerAttributes(&$field) { | |
be2fb01f CW |
2004 | $attributes = []; |
2005 | $dateAttributes = [ | |
d86e674f | 2006 | 'start_date_years' => 'minYear', |
2007 | 'end_date_years' => 'maxYear', | |
2008 | 'date_format' => 'format', | |
be2fb01f | 2009 | ]; |
d86e674f | 2010 | foreach ($dateAttributes as $dateAttribute => $mapTo) { |
2011 | if (isset($field[$dateAttribute])) { | |
2012 | $attributes[$mapTo] = $field[$dateAttribute]; | |
2013 | } | |
2014 | } | |
2015 | return $attributes; | |
2016 | } | |
2017 | ||
6a488035 | 2018 | /** |
50bfb460 | 2019 | * Function to convert mysql to date plugin format. |
6a488035 | 2020 | * |
77855840 TO |
2021 | * @param string $mysqlDate |
2022 | * Date string. | |
f4aaa82a EM |
2023 | * |
2024 | * @param null $formatType | |
2025 | * @param null $format | |
2026 | * @param null $timeFormat | |
6a488035 | 2027 | * |
a6c01b45 CW |
2028 | * @return array |
2029 | * and time | |
6a488035 | 2030 | */ |
00be9182 | 2031 | public static function setDateDefaults($mysqlDate = NULL, $formatType = NULL, $format = NULL, $timeFormat = NULL) { |
6a488035 TO |
2032 | // if date is not passed assume it as today |
2033 | if (!$mysqlDate) { | |
2034 | $mysqlDate = date('Y-m-d G:i:s'); | |
2035 | } | |
2036 | ||
2037 | $config = CRM_Core_Config::singleton(); | |
2038 | if ($formatType) { | |
2039 | // get actual format | |
be2fb01f CW |
2040 | $params = ['name' => $formatType]; |
2041 | $values = []; | |
6a488035 TO |
2042 | CRM_Core_DAO::commonRetrieve('CRM_Core_DAO_PreferencesDate', $params, $values); |
2043 | ||
2044 | if ($values['date_format']) { | |
2045 | $format = $values['date_format']; | |
2046 | } | |
2047 | ||
2048 | if (isset($values['time_format'])) { | |
2049 | $timeFormat = $values['time_format']; | |
2050 | } | |
2051 | } | |
2052 | ||
6a488035 TO |
2053 | $dateFormat = 'm/d/Y'; |
2054 | $date = date($dateFormat, strtotime($mysqlDate)); | |
2055 | ||
2056 | if (!$timeFormat) { | |
2057 | $timeFormat = $config->timeInputFormat; | |
2058 | } | |
2059 | ||
2060 | $actualTimeFormat = "g:iA"; | |
2061 | $appendZeroLength = 7; | |
2062 | if ($timeFormat > 1) { | |
2063 | $actualTimeFormat = "G:i"; | |
2064 | $appendZeroLength = 5; | |
2065 | } | |
2066 | ||
2067 | $time = date($actualTimeFormat, strtotime($mysqlDate)); | |
2068 | ||
2069 | // need to append zero for hours < 10 | |
2070 | if (strlen($time) < $appendZeroLength) { | |
2071 | $time = '0' . $time; | |
2072 | } | |
2073 | ||
be2fb01f | 2074 | return [$date, $time]; |
6a488035 TO |
2075 | } |
2076 | ||
2077 | /** | |
fe482240 | 2078 | * Function get date format. |
6a488035 | 2079 | * |
77855840 TO |
2080 | * @param string $formatType |
2081 | * Date name e.g. birth. | |
6a488035 | 2082 | * |
a6c01b45 | 2083 | * @return string |
6a488035 | 2084 | */ |
00be9182 | 2085 | public static function getDateFormat($formatType = NULL) { |
6a488035 TO |
2086 | $format = NULL; |
2087 | if ($formatType) { | |
395d8dc6 | 2088 | $format = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_PreferencesDate', |
353ffa53 | 2089 | $formatType, 'date_format', 'name' |
6a488035 TO |
2090 | ); |
2091 | } | |
2092 | ||
2093 | if (!$format) { | |
2094 | $config = CRM_Core_Config::singleton(); | |
2095 | $format = $config->dateInputFormat; | |
2096 | } | |
2097 | return $format; | |
2098 | } | |
2099 | ||
5bc392e6 EM |
2100 | /** |
2101 | * @param $date | |
2102 | * @param $dateType | |
2103 | * | |
2104 | * @return null|string | |
2105 | */ | |
00be9182 | 2106 | public static function formatDate($date, $dateType) { |
6a488035 TO |
2107 | $formattedDate = NULL; |
2108 | if (empty($date)) { | |
2109 | return $formattedDate; | |
2110 | } | |
2111 | ||
50bfb460 SB |
2112 | // 1. first convert date to default format. |
2113 | // 2. append time to default formatted date (might be removed during format) | |
2114 | // 3. validate date / date time. | |
2115 | // 4. If date and time then convert to default date time format. | |
6a488035 TO |
2116 | |
2117 | $dateKey = 'date'; | |
be2fb01f | 2118 | $dateParams = [$dateKey => $date]; |
6a488035 TO |
2119 | |
2120 | if (CRM_Utils_Date::convertToDefaultDate($dateParams, $dateType, $dateKey)) { | |
2121 | $dateVal = $dateParams[$dateKey]; | |
2122 | $ruleName = 'date'; | |
2123 | if ($dateType == 1) { | |
be2fb01f | 2124 | $matches = []; |
6a488035 TO |
2125 | if (preg_match("/(\s(([01]\d)|[2][0-3]):([0-5]\d))$/", $date, $matches)) { |
2126 | $ruleName = 'dateTime'; | |
2127 | if (strpos($date, '-') !== FALSE) { | |
2128 | $dateVal .= array_shift($matches); | |
2129 | } | |
2130 | } | |
2131 | } | |
2132 | ||
2133 | // validate date. | |
0e6e8724 | 2134 | $valid = CRM_Utils_Rule::$ruleName($dateVal); |
6a488035 TO |
2135 | |
2136 | if ($valid) { | |
50bfb460 | 2137 | // format date and time to default. |
6a488035 TO |
2138 | if ($ruleName == 'dateTime') { |
2139 | $dateVal = CRM_Utils_Date::customFormat(preg_replace("/(:|\s)?/", "", $dateVal), '%Y%m%d%H%i'); | |
50bfb460 | 2140 | // hack to add seconds |
6a488035 TO |
2141 | $dateVal .= '00'; |
2142 | } | |
2143 | $formattedDate = $dateVal; | |
2144 | } | |
2145 | } | |
2146 | ||
2147 | return $formattedDate; | |
2148 | } | |
2149 | ||
dccd9f4f ERL |
2150 | /** |
2151 | * Function to return days of the month. | |
2152 | * | |
2153 | * @return array | |
2154 | */ | |
2155 | public static function getCalendarDayOfMonth() { | |
be2fb01f | 2156 | $month = []; |
dccd9f4f ERL |
2157 | for ($i = 1; $i <= 31; $i++) { |
2158 | $month[$i] = $i; | |
2159 | if ($i == 31) { | |
2160 | $month[$i] = $i . ' / Last day of month'; | |
2161 | } | |
2162 | } | |
2163 | return $month; | |
2164 | } | |
2165 | ||
6e793248 | 2166 | /** |
2167 | * Convert a relative date format to an api field. | |
2168 | * | |
2169 | * @param array $params | |
2170 | * @param string $dateField | |
2171 | * @param bool $isDatePicker | |
2172 | * Non datepicker fields are deprecated. Exterminate Exterminate. | |
2173 | * (but for now handle them). | |
2174 | */ | |
2175 | public static function convertFormDateToApiFormat(&$params, $dateField, $isDatePicker = TRUE) { | |
2176 | if (!empty($params[$dateField . '_relative'])) { | |
2177 | $dates = CRM_Utils_Date::getFromTo($params[$dateField . '_relative'], NULL, NULL); | |
2178 | unset($params[$dateField . '_relative']); | |
2179 | } | |
2180 | if (!empty($params[$dateField . '_low'])) { | |
2181 | $dates[0] = $isDatePicker ? $params[$dateField . '_low'] : date('Y-m-d H:i:s', strtotime($params[$dateField . '_low'])); | |
2182 | unset($params[$dateField . '_low']); | |
2183 | } | |
2184 | if (!empty($params[$dateField . '_high'])) { | |
2185 | $dates[1] = $isDatePicker ? $params[$dateField . '_high'] : date('Y-m-d 23:59:59', strtotime($params[$dateField . '_high'])); | |
2186 | unset($params[$dateField . '_high']); | |
2187 | } | |
2188 | if (empty($dates)) { | |
2189 | return; | |
2190 | } | |
2191 | if (empty($dates[0])) { | |
2192 | $params[$dateField] = ['<=' => $dates[1]]; | |
2193 | } | |
2194 | elseif (empty($dates[1])) { | |
2195 | $params[$dateField] = ['>=' => $dates[0]]; | |
2196 | } | |
2197 | else { | |
2198 | $params[$dateField] = ['BETWEEN' => $dates]; | |
2199 | } | |
2200 | } | |
2201 | ||
602836ee SL |
2202 | /** |
2203 | * Print out a date object in specified format in local timezone | |
2204 | * | |
2205 | * @param DateTimeObject $dateObject | |
2206 | * @param string $format | |
2207 | * @return string | |
2208 | */ | |
2209 | public static function convertDateToLocalTime($dateObject, $format = 'YmdHis') { | |
2210 | $systemTimeZone = new DateTimeZone(CRM_Core_Config::singleton()->userSystem->getTimeZoneString()); | |
2211 | $dateObject->setTimezone($systemTimeZone); | |
2212 | return $dateObject->format($format); | |
2213 | } | |
2214 | ||
6a488035 | 2215 | } |