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