Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
fee14197 | 4 | | CiviCRM version 5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
6b83d5bd | 6 | | Copyright CiviCRM LLC (c) 2004-2019 | |
6a488035 TO |
7 | +--------------------------------------------------------------------+ |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
10 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
11 | | under the terms of the GNU Affero General Public License | | |
12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
13 | | | | |
14 | | CiviCRM is distributed in the hope that it will be useful, but | | |
15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
17 | | See the GNU Affero General Public License for more details. | | |
18 | | | | |
19 | | You should have received a copy of the GNU Affero General Public | | |
20 | | License and the CiviCRM Licensing Exception along | | |
21 | | with this program; if not, contact CiviCRM LLC | | |
22 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
23 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
25 | +--------------------------------------------------------------------+ | |
e70a7fc0 | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
6b83d5bd | 31 | * @copyright CiviCRM LLC (c) 2004-2019 |
6a488035 TO |
32 | */ |
33 | ||
34 | /** | |
35 | * Date utilties | |
36 | */ | |
37 | class CRM_Utils_Date { | |
38 | ||
39 | /** | |
100fef9d | 40 | * Format a date by padding it with leading '0'. |
6a488035 | 41 | * |
77855840 TO |
42 | * @param array $date |
43 | * ('Y', 'M', 'd'). | |
44 | * @param string $separator | |
45 | * The seperator to use when formatting the date. | |
f4aaa82a | 46 | * @param int|string $invalidDate what to return if the date is invalid |
6a488035 | 47 | * |
a6c01b45 CW |
48 | * @return string |
49 | * formatted string for date | |
6a488035 | 50 | */ |
00be9182 | 51 | public static function format($date, $separator = '', $invalidDate = 0) { |
6a488035 TO |
52 | if (is_numeric($date) && |
53 | ((strlen($date) == 8) || (strlen($date) == 14)) | |
54 | ) { | |
55 | return $date; | |
56 | } | |
57 | ||
58 | if (!is_array($date) || | |
59 | CRM_Utils_System::isNull($date) || | |
60 | empty($date['Y']) | |
61 | ) { | |
62 | return $invalidDate; | |
63 | } | |
64 | ||
65 | $date['Y'] = (int ) $date['Y']; | |
66 | if ($date['Y'] < 1000 || $date['Y'] > 2999) { | |
67 | return $invalidDate; | |
68 | } | |
69 | ||
70 | if (array_key_exists('m', $date)) { | |
71 | $date['M'] = $date['m']; | |
72 | } | |
73 | elseif (array_key_exists('F', $date)) { | |
74 | $date['M'] = $date['F']; | |
75 | } | |
76 | ||
a7488080 | 77 | if (!empty($date['M'])) { |
6a488035 TO |
78 | $date['M'] = (int ) $date['M']; |
79 | if ($date['M'] < 1 || $date['M'] > 12) { | |
80 | return $invalidDate; | |
81 | } | |
82 | } | |
83 | else { | |
84 | $date['M'] = 1; | |
85 | } | |
86 | ||
a7488080 | 87 | if (!empty($date['d'])) { |
6a488035 TO |
88 | $date['d'] = (int ) $date['d']; |
89 | } | |
90 | else { | |
91 | $date['d'] = 1; | |
92 | } | |
93 | ||
94 | if (!checkdate($date['M'], $date['d'], $date['Y'])) { | |
95 | return $invalidDate; | |
96 | } | |
97 | ||
98 | $date['M'] = sprintf('%02d', $date['M']); | |
99 | $date['d'] = sprintf('%02d', $date['d']); | |
100 | ||
101 | $time = ''; | |
102 | if (CRM_Utils_Array::value('H', $date) != NULL || | |
103 | CRM_Utils_Array::value('h', $date) != NULL || | |
104 | CRM_Utils_Array::value('i', $date) != NULL || | |
105 | CRM_Utils_Array::value('s', $date) != NULL | |
106 | ) { | |
107 | // we have time too.. | |
a7488080 | 108 | if (!empty($date['h'])) { |
6a488035 TO |
109 | if (CRM_Utils_Array::value('A', $date) == 'PM' or CRM_Utils_Array::value('a', $date) == 'pm') { |
110 | if ($date['h'] != 12) { | |
111 | $date['h'] = $date['h'] + 12; | |
112 | } | |
113 | } | |
114 | if ((CRM_Utils_Array::value('A', $date) == 'AM' or CRM_Utils_Array::value('a', $date) == 'am') && | |
115 | CRM_Utils_Array::value('h', $date) == 12 | |
116 | ) { | |
117 | $date['h'] = '00'; | |
118 | } | |
119 | ||
120 | $date['h'] = (int ) $date['h']; | |
121 | } | |
122 | else { | |
123 | $date['h'] = 0; | |
124 | } | |
125 | ||
126 | // in 24-hour format the hour is under the 'H' key | |
a7488080 | 127 | if (!empty($date['H'])) { |
6a488035 TO |
128 | $date['H'] = (int) $date['H']; |
129 | } | |
130 | else { | |
131 | $date['H'] = 0; | |
132 | } | |
133 | ||
a7488080 | 134 | if (!empty($date['i'])) { |
6a488035 TO |
135 | $date['i'] = (int ) $date['i']; |
136 | } | |
137 | else { | |
138 | $date['i'] = 0; | |
139 | } | |
140 | ||
141 | if ($date['h'] == 0 && $date['H'] != 0) { | |
142 | $date['h'] = $date['H']; | |
143 | } | |
144 | ||
a7488080 | 145 | if (!empty($date['s'])) { |
6a488035 TO |
146 | $date['s'] = (int ) $date['s']; |
147 | } | |
148 | else { | |
149 | $date['s'] = 0; | |
150 | } | |
151 | ||
152 | $date['h'] = sprintf('%02d', $date['h']); | |
153 | $date['i'] = sprintf('%02d', $date['i']); | |
154 | $date['s'] = sprintf('%02d', $date['s']); | |
155 | ||
156 | if ($separator) { | |
157 | $time = ' '; | |
158 | } | |
159 | $time .= $date['h'] . $separator . $date['i'] . $separator . $date['s']; | |
160 | } | |
161 | ||
162 | return $date['Y'] . $separator . $date['M'] . $separator . $date['d'] . $time; | |
163 | } | |
164 | ||
165 | /** | |
fe482240 | 166 | * Return abbreviated weekday names according to the locale. |
6a488035 | 167 | * |
1ef73b88 CW |
168 | * Array will be in localized order according to 'weekBegins' setting, |
169 | * but array keys will always match to: | |
170 | * 0 => Sun | |
171 | * 1 => Mon | |
172 | * etc. | |
173 | * | |
a6c01b45 CW |
174 | * @return array |
175 | * 0-based array with abbreviated weekday names | |
6a488035 | 176 | * |
6a488035 | 177 | */ |
1ef73b88 | 178 | public static function getAbbrWeekdayNames() { |
be2fb01f | 179 | static $days = []; |
1ef73b88 CW |
180 | if (!$days) { |
181 | // First day of the week | |
aaffa79f | 182 | $firstDay = Civi::settings()->get('weekBegins'); |
6a488035 TO |
183 | |
184 | // set LC_TIME and build the arrays from locale-provided names | |
185 | // June 1st, 1970 was a Monday | |
186 | CRM_Core_I18n::setLcTime(); | |
41ff54d3 | 187 | for ($i = $firstDay; count($days) < 7; $i = $i > 5 ? 0 : $i + 1) { |
1ef73b88 | 188 | $days[$i] = strftime('%a', mktime(0, 0, 0, 6, $i, 1970)); |
6a488035 TO |
189 | } |
190 | } | |
1ef73b88 | 191 | return $days; |
6a488035 TO |
192 | } |
193 | ||
194 | /** | |
fe482240 | 195 | * Return full weekday names according to the locale. |
6a488035 | 196 | * |
1ef73b88 CW |
197 | * Array will be in localized order according to 'weekBegins' setting, |
198 | * but array keys will always match to: | |
199 | * 0 => Sunday | |
200 | * 1 => Monday | |
201 | * etc. | |
202 | * | |
a6c01b45 CW |
203 | * @return array |
204 | * 0-based array with full weekday names | |
6a488035 | 205 | * |
6a488035 | 206 | */ |
1ef73b88 | 207 | public static function getFullWeekdayNames() { |
be2fb01f | 208 | static $days = []; |
1ef73b88 CW |
209 | if (!$days) { |
210 | // First day of the week | |
aaffa79f | 211 | $firstDay = Civi::settings()->get('weekBegins'); |
6a488035 TO |
212 | |
213 | // set LC_TIME and build the arrays from locale-provided names | |
214 | // June 1st, 1970 was a Monday | |
215 | CRM_Core_I18n::setLcTime(); | |
41ff54d3 | 216 | for ($i = $firstDay; count($days) < 7; $i = $i > 5 ? 0 : $i + 1) { |
1ef73b88 | 217 | $days[$i] = strftime('%A', mktime(0, 0, 0, 6, $i, 1970)); |
6a488035 TO |
218 | } |
219 | } | |
1ef73b88 | 220 | return $days; |
6a488035 TO |
221 | } |
222 | ||
223 | /** | |
fe482240 | 224 | * Return abbreviated month names according to the locale. |
6a488035 | 225 | * |
f4aaa82a EM |
226 | * @param bool $month |
227 | * | |
a6c01b45 CW |
228 | * @return array |
229 | * 1-based array with abbreviated month names | |
6a488035 | 230 | * |
6a488035 | 231 | */ |
00be9182 | 232 | public static function &getAbbrMonthNames($month = FALSE) { |
6a488035 TO |
233 | static $abbrMonthNames; |
234 | if (!isset($abbrMonthNames)) { | |
235 | ||
236 | // set LC_TIME and build the arrays from locale-provided names | |
237 | CRM_Core_I18n::setLcTime(); | |
238 | for ($i = 1; $i <= 12; $i++) { | |
239 | $abbrMonthNames[$i] = strftime('%b', mktime(0, 0, 0, $i, 10, 1970)); | |
240 | } | |
241 | } | |
242 | if ($month) { | |
243 | return $abbrMonthNames[$month]; | |
244 | } | |
245 | return $abbrMonthNames; | |
246 | } | |
247 | ||
248 | /** | |
fe482240 | 249 | * Return full month names according to the locale. |
6a488035 | 250 | * |
a6c01b45 CW |
251 | * @return array |
252 | * 1-based array with full month names | |
6a488035 | 253 | * |
6a488035 | 254 | */ |
00be9182 | 255 | public static function &getFullMonthNames() { |
6a488035 TO |
256 | static $fullMonthNames; |
257 | if (!isset($fullMonthNames)) { | |
258 | ||
259 | // set LC_TIME and build the arrays from locale-provided names | |
260 | CRM_Core_I18n::setLcTime(); | |
261 | for ($i = 1; $i <= 12; $i++) { | |
262 | $fullMonthNames[$i] = strftime('%B', mktime(0, 0, 0, $i, 10, 1970)); | |
263 | } | |
264 | } | |
265 | return $fullMonthNames; | |
266 | } | |
267 | ||
5bc392e6 EM |
268 | /** |
269 | * @param $string | |
270 | * | |
271 | * @return int | |
272 | */ | |
00be9182 | 273 | public static function unixTime($string) { |
6a488035 TO |
274 | if (empty($string)) { |
275 | return 0; | |
276 | } | |
277 | $parsedDate = date_parse($string); | |
278 | return mktime(CRM_Utils_Array::value('hour', $parsedDate), | |
279 | CRM_Utils_Array::value('minute', $parsedDate), | |
280 | 59, | |
281 | CRM_Utils_Array::value('month', $parsedDate), | |
282 | CRM_Utils_Array::value('day', $parsedDate), | |
283 | CRM_Utils_Array::value('year', $parsedDate) | |
284 | ); | |
285 | } | |
286 | ||
287 | /** | |
fe482240 | 288 | * Create a date and time string in a provided format. |
6a488035 TO |
289 | * |
290 | * %b - abbreviated month name ('Jan'..'Dec') | |
291 | * %B - full month name ('January'..'December') | |
292 | * %d - day of the month as a decimal number, 0-padded ('01'..'31') | |
293 | * %e - day of the month as a decimal number, blank-padded (' 1'..'31') | |
294 | * %E - day of the month as a decimal number ('1'..'31') | |
295 | * %f - English ordinal suffix for the day of the month ('st', 'nd', 'rd', 'th') | |
296 | * %H - hour in 24-hour format, 0-padded ('00'..'23') | |
297 | * %I - hour in 12-hour format, 0-padded ('01'..'12') | |
298 | * %k - hour in 24-hour format, blank-padded (' 0'..'23') | |
299 | * %l - hour in 12-hour format, blank-padded (' 1'..'12') | |
300 | * %m - month as a decimal number, 0-padded ('01'..'12') | |
301 | * %M - minute, 0-padded ('00'..'60') | |
302 | * %p - lowercase ante/post meridiem ('am', 'pm') | |
303 | * %P - uppercase ante/post meridiem ('AM', 'PM') | |
304 | * %Y - year as a decimal number including the century ('2005') | |
305 | * | |
77855840 TO |
306 | * @param string $dateString |
307 | * Date and time in 'YYYY-MM-DD hh:mm:ss' format. | |
308 | * @param string $format | |
309 | * The output format. | |
310 | * @param array $dateParts | |
311 | * An array with the desired date parts. | |
6a488035 | 312 | * |
a6c01b45 CW |
313 | * @return string |
314 | * the $format-formatted $date | |
6a488035 | 315 | */ |
00be9182 | 316 | public static function customFormat($dateString, $format = NULL, $dateParts = NULL) { |
6a488035 TO |
317 | // 1-based (January) month names arrays |
318 | $abbrMonths = self::getAbbrMonthNames(); | |
319 | $fullMonths = self::getFullMonthNames(); | |
320 | ||
321 | if (!$format) { | |
322 | $config = CRM_Core_Config::singleton(); | |
323 | ||
324 | if ($dateParts) { | |
be2fb01f | 325 | if (array_intersect(['h', 'H'], $dateParts)) { |
6a488035 TO |
326 | $format = $config->dateformatDatetime; |
327 | } | |
be2fb01f | 328 | elseif (array_intersect(['d', 'j'], $dateParts)) { |
6a488035 TO |
329 | $format = $config->dateformatFull; |
330 | } | |
be2fb01f | 331 | elseif (array_intersect(['m', 'M'], $dateParts)) { |
6a488035 TO |
332 | $format = $config->dateformatPartial; |
333 | } | |
334 | else { | |
335 | $format = $config->dateformatYear; | |
336 | } | |
337 | } | |
338 | else { | |
339 | if (strpos($dateString, '-')) { | |
340 | $month = (int) substr($dateString, 5, 2); | |
341 | $day = (int) substr($dateString, 8, 2); | |
342 | } | |
343 | else { | |
344 | $month = (int) substr($dateString, 4, 2); | |
345 | $day = (int) substr($dateString, 6, 2); | |
346 | } | |
347 | ||
348 | if (strlen($dateString) > 10) { | |
349 | $format = $config->dateformatDatetime; | |
350 | } | |
351 | elseif ($day > 0) { | |
352 | $format = $config->dateformatFull; | |
353 | } | |
354 | elseif ($month > 0) { | |
355 | $format = $config->dateformatPartial; | |
356 | } | |
357 | else { | |
358 | $format = $config->dateformatYear; | |
359 | } | |
360 | } | |
361 | } | |
362 | ||
35fa23f8 | 363 | if (!CRM_Utils_System::isNull($dateString)) { |
6a488035 | 364 | if (strpos($dateString, '-')) { |
353ffa53 | 365 | $year = (int) substr($dateString, 0, 4); |
6a488035 | 366 | $month = (int) substr($dateString, 5, 2); |
353ffa53 | 367 | $day = (int) substr($dateString, 8, 2); |
6a488035 TO |
368 | |
369 | $hour24 = (int) substr($dateString, 11, 2); | |
370 | $minute = (int) substr($dateString, 14, 2); | |
371 | } | |
372 | else { | |
353ffa53 | 373 | $year = (int) substr($dateString, 0, 4); |
6a488035 | 374 | $month = (int) substr($dateString, 4, 2); |
353ffa53 | 375 | $day = (int) substr($dateString, 6, 2); |
6a488035 TO |
376 | |
377 | $hour24 = (int) substr($dateString, 8, 2); | |
378 | $minute = (int) substr($dateString, 10, 2); | |
379 | } | |
380 | ||
381 | if ($day % 10 == 1 and $day != 11) { | |
382 | $suffix = 'st'; | |
383 | } | |
384 | elseif ($day % 10 == 2 and $day != 12) { | |
385 | $suffix = 'nd'; | |
386 | } | |
387 | elseif ($day % 10 == 3 and $day != 13) { | |
388 | $suffix = 'rd'; | |
389 | } | |
390 | else { | |
391 | $suffix = 'th'; | |
392 | } | |
393 | ||
394 | if ($hour24 < 12) { | |
395 | if ($hour24 == 00) { | |
396 | $hour12 = 12; | |
397 | } | |
398 | else { | |
399 | $hour12 = $hour24; | |
400 | } | |
401 | $type = 'AM'; | |
402 | } | |
403 | else { | |
404 | if ($hour24 == 12) { | |
405 | $hour12 = 12; | |
406 | } | |
407 | else { | |
408 | $hour12 = $hour24 - 12; | |
409 | } | |
410 | $type = 'PM'; | |
411 | } | |
412 | ||
be2fb01f | 413 | $date = [ |
6a488035 TO |
414 | '%b' => CRM_Utils_Array::value($month, $abbrMonths), |
415 | '%B' => CRM_Utils_Array::value($month, $fullMonths), | |
416 | '%d' => $day > 9 ? $day : '0' . $day, | |
417 | '%e' => $day > 9 ? $day : ' ' . $day, | |
418 | '%E' => $day, | |
419 | '%f' => $suffix, | |
420 | '%H' => $hour24 > 9 ? $hour24 : '0' . $hour24, | |
421 | '%h' => $hour12 > 9 ? $hour12 : '0' . $hour12, | |
422 | '%I' => $hour12 > 9 ? $hour12 : '0' . $hour12, | |
423 | '%k' => $hour24 > 9 ? $hour24 : ' ' . $hour24, | |
424 | '%l' => $hour12 > 9 ? $hour12 : ' ' . $hour12, | |
425 | '%m' => $month > 9 ? $month : '0' . $month, | |
426 | '%M' => $minute > 9 ? $minute : '0' . $minute, | |
427 | '%i' => $minute > 9 ? $minute : '0' . $minute, | |
428 | '%p' => strtolower($type), | |
429 | '%P' => $type, | |
430 | '%A' => $type, | |
431 | '%Y' => $year, | |
be2fb01f | 432 | ]; |
6a488035 TO |
433 | |
434 | return strtr($format, $date); | |
435 | } | |
436 | else { | |
437 | return ''; | |
438 | } | |
439 | } | |
440 | ||
d2f262da AS |
441 | /** |
442 | * Wrapper for customFormat that takes a timestamp | |
443 | * | |
444 | * @param int $timestamp | |
445 | * Date and time in timestamp format. | |
446 | * @param string $format | |
447 | * The output format. | |
448 | * @param array $dateParts | |
449 | * An array with the desired date parts. | |
450 | * | |
451 | * @return string | |
452 | * the $format-formatted $date | |
453 | */ | |
454 | public static function customFormatTs($timestamp, $format = NULL, $dateParts = NULL) { | |
455 | return CRM_Utils_Date::customFormat(date("Y-m-d H:i:s", $timestamp), $format, $dateParts); | |
456 | } | |
457 | ||
6a488035 | 458 | /** |
100fef9d | 459 | * Converts the date/datetime from MySQL format to ISO format |
6a488035 | 460 | * |
77855840 TO |
461 | * @param string $mysql |
462 | * Date/datetime in MySQL format. | |
6a488035 | 463 | * |
a6c01b45 CW |
464 | * @return string |
465 | * date/datetime in ISO format | |
6a488035 | 466 | */ |
00be9182 | 467 | public static function mysqlToIso($mysql) { |
353ffa53 TO |
468 | $year = substr($mysql, 0, 4); |
469 | $month = substr($mysql, 4, 2); | |
470 | $day = substr($mysql, 6, 2); | |
471 | $hour = substr($mysql, 8, 2); | |
6a488035 TO |
472 | $minute = substr($mysql, 10, 2); |
473 | $second = substr($mysql, 12, 2); | |
474 | ||
475 | $iso = ''; | |
476 | if ($year) { | |
477 | $iso .= "$year"; | |
478 | } | |
479 | if ($month) { | |
480 | $iso .= "-$month"; | |
481 | if ($day) { | |
482 | $iso .= "-$day"; | |
483 | } | |
484 | } | |
485 | ||
486 | if ($hour) { | |
487 | $iso .= " $hour"; | |
488 | if ($minute) { | |
489 | $iso .= ":$minute"; | |
490 | if ($second) { | |
491 | $iso .= ":$second"; | |
492 | } | |
493 | } | |
494 | } | |
495 | return $iso; | |
496 | } | |
497 | ||
498 | /** | |
100fef9d | 499 | * Converts the date/datetime from ISO format to MySQL format |
5d9f6898 EM |
500 | * Note that until CRM-14986/ 4.4.7 this was required whenever the pattern $dao->find(TRUE): $dao->save(); was |
501 | * used to update an object with a date field was used. The DAO now checks for a '-' in date field strings | |
502 | * & runs this function if the - appears - meaning it is likely redundant in the form & BAO layers | |
6a488035 | 503 | * |
77855840 TO |
504 | * @param string $iso |
505 | * Date/datetime in ISO format. | |
6a488035 | 506 | * |
a6c01b45 CW |
507 | * @return string |
508 | * date/datetime in MySQL format | |
6a488035 | 509 | */ |
00be9182 | 510 | public static function isoToMysql($iso) { |
be2fb01f | 511 | $dropArray = ['-' => '', ':' => '', ' ' => '']; |
6a488035 TO |
512 | return strtr($iso, $dropArray); |
513 | } | |
514 | ||
515 | /** | |
100fef9d | 516 | * Converts the any given date to default date format. |
6a488035 | 517 | * |
77855840 TO |
518 | * @param array $params |
519 | * Has given date-format. | |
520 | * @param int $dateType | |
521 | * Type of date. | |
522 | * @param string $dateParam | |
523 | * Index of params. | |
f4aaa82a EM |
524 | * |
525 | * @return bool | |
6a488035 | 526 | */ |
00be9182 | 527 | public static function convertToDefaultDate(&$params, $dateType, $dateParam) { |
353ffa53 TO |
528 | $now = getdate(); |
529 | $cen = substr($now['year'], 0, 2); | |
6a488035 TO |
530 | $prevCen = $cen - 1; |
531 | ||
532 | $value = NULL; | |
a7488080 | 533 | if (!empty($params[$dateParam])) { |
6a488035 TO |
534 | // suppress hh:mm or hh:mm:ss if it exists CRM-7957 |
535 | $value = preg_replace("/(\s(([01]\d)|[2][0-3])(:([0-5]\d)){1,2})$/", "", $params[$dateParam]); | |
536 | } | |
537 | ||
538 | switch ($dateType) { | |
539 | case 1: | |
540 | if (!preg_match('/^\d\d\d\d-?(\d|\d\d)-?(\d|\d\d)$/', $value)) { | |
541 | return FALSE; | |
542 | } | |
543 | break; | |
544 | ||
545 | case 2: | |
546 | if (!preg_match('/^(\d|\d\d)[-\/](\d|\d\d)[-\/]\d\d$/', $value)) { | |
547 | return FALSE; | |
548 | } | |
549 | break; | |
550 | ||
551 | case 4: | |
552 | if (!preg_match('/^(\d|\d\d)[-\/](\d|\d\d)[-\/]\d\d\d\d$/', $value)) { | |
553 | return FALSE; | |
554 | } | |
555 | break; | |
556 | ||
557 | case 8: | |
558 | if (!preg_match('/^[A-Za-z]*.[ \t]?\d\d\,[ \t]?\d\d\d\d$/', $value)) { | |
559 | return FALSE; | |
560 | } | |
561 | break; | |
562 | ||
563 | case 16: | |
564 | if (!preg_match('/^\d\d-[A-Za-z]{3}.*-\d\d$/', $value) && !preg_match('/^\d\d[-\/]\d\d[-\/]\d\d$/', $value)) { | |
565 | return FALSE; | |
566 | } | |
567 | break; | |
568 | ||
569 | case 32: | |
570 | if (!preg_match('/^(\d|\d\d)[-\/](\d|\d\d)[-\/]\d\d\d\d/', $value)) { | |
571 | return FALSE; | |
572 | } | |
573 | break; | |
574 | } | |
575 | ||
576 | if ($dateType == 1) { | |
577 | $formattedDate = explode("-", $value); | |
578 | if (count($formattedDate) == 3) { | |
353ffa53 | 579 | $year = (int) $formattedDate[0]; |
6a488035 | 580 | $month = (int) $formattedDate[1]; |
353ffa53 | 581 | $day = (int) $formattedDate[2]; |
6a488035 TO |
582 | } |
583 | elseif (count($formattedDate) == 1 && (strlen($value) == 8)) { | |
584 | return TRUE; | |
585 | } | |
586 | else { | |
587 | return FALSE; | |
588 | } | |
589 | } | |
590 | ||
6a488035 TO |
591 | if ($dateType == 2 || $dateType == 4) { |
592 | $formattedDate = explode("/", $value); | |
593 | if (count($formattedDate) != 3) { | |
594 | $formattedDate = explode("-", $value); | |
595 | } | |
596 | if (count($formattedDate) == 3) { | |
353ffa53 | 597 | $year = (int) $formattedDate[2]; |
6a488035 | 598 | $month = (int) $formattedDate[0]; |
353ffa53 | 599 | $day = (int) $formattedDate[1]; |
6a488035 TO |
600 | } |
601 | else { | |
602 | return FALSE; | |
603 | } | |
604 | } | |
605 | if ($dateType == 8) { | |
606 | $dateArray = explode(' ', $value); | |
607 | // ignore comma(,) | |
608 | $dateArray[1] = (int) substr($dateArray[1], 0, 2); | |
609 | ||
610 | $monthInt = 0; | |
611 | $fullMonths = self::getFullMonthNames(); | |
612 | foreach ($fullMonths as $key => $val) { | |
613 | if (strtolower($dateArray[0]) == strtolower($val)) { | |
614 | $monthInt = $key; | |
615 | break; | |
616 | } | |
617 | } | |
618 | if (!$monthInt) { | |
619 | $abbrMonths = self::getAbbrMonthNames(); | |
620 | foreach ($abbrMonths as $key => $val) { | |
621 | if (strtolower(trim($dateArray[0], ".")) == strtolower($val)) { | |
622 | $monthInt = $key; | |
623 | break; | |
624 | } | |
625 | } | |
626 | } | |
353ffa53 TO |
627 | $year = (int) $dateArray[2]; |
628 | $day = (int) $dateArray[1]; | |
6a488035 TO |
629 | $month = (int) $monthInt; |
630 | } | |
631 | if ($dateType == 16) { | |
632 | $dateArray = explode('-', $value); | |
633 | if (count($dateArray) != 3) { | |
634 | $dateArray = explode('/', $value); | |
635 | } | |
636 | ||
637 | if (count($dateArray) == 3) { | |
638 | $monthInt = 0; | |
639 | $fullMonths = self::getFullMonthNames(); | |
640 | foreach ($fullMonths as $key => $val) { | |
641 | if (strtolower($dateArray[1]) == strtolower($val)) { | |
642 | $monthInt = $key; | |
643 | break; | |
644 | } | |
645 | } | |
646 | if (!$monthInt) { | |
647 | $abbrMonths = self::getAbbrMonthNames(); | |
648 | foreach ($abbrMonths as $key => $val) { | |
649 | if (strtolower(trim($dateArray[1], ".")) == strtolower($val)) { | |
650 | $monthInt = $key; | |
651 | break; | |
652 | } | |
653 | } | |
654 | } | |
655 | if (!$monthInt) { | |
656 | $monthInt = $dateArray[1]; | |
657 | } | |
658 | ||
353ffa53 TO |
659 | $year = (int) $dateArray[2]; |
660 | $day = (int) $dateArray[0]; | |
6a488035 TO |
661 | $month = (int) $monthInt; |
662 | } | |
663 | else { | |
664 | return FALSE; | |
665 | } | |
666 | } | |
667 | if ($dateType == 32) { | |
668 | $formattedDate = explode("/", $value); | |
669 | if (count($formattedDate) == 3) { | |
353ffa53 | 670 | $year = (int) $formattedDate[2]; |
6a488035 | 671 | $month = (int) $formattedDate[1]; |
353ffa53 | 672 | $day = (int) $formattedDate[0]; |
6a488035 TO |
673 | } |
674 | else { | |
675 | return FALSE; | |
676 | } | |
677 | } | |
678 | ||
679 | $month = ($month < 10) ? "0" . "$month" : $month; | |
680 | $day = ($day < 10) ? "0" . "$day" : $day; | |
681 | ||
682 | $year = (int ) $year; | |
683 | // simple heuristic to determine what century to use | |
684 | // 00 - 20 is always 2000 - 2020 | |
685 | // 21 - 99 is always 1921 - 1999 | |
686 | if ($year < 21) { | |
687 | $year = (strlen($year) == 1) ? $cen . '0' . $year : $cen . $year; | |
688 | } | |
689 | elseif ($year < 100) { | |
690 | $year = $prevCen . $year; | |
691 | } | |
692 | ||
693 | if ($params[$dateParam]) { | |
694 | $params[$dateParam] = "$year$month$day"; | |
695 | } | |
50bfb460 | 696 | // if month is invalid return as error |
6a488035 TO |
697 | if ($month !== '00' && $month <= 12) { |
698 | return TRUE; | |
699 | } | |
700 | return FALSE; | |
701 | } | |
702 | ||
5bc392e6 EM |
703 | /** |
704 | * @param $date | |
705 | * | |
706 | * @return bool | |
707 | */ | |
00be9182 | 708 | public static function isDate(&$date) { |
6a488035 TO |
709 | if (CRM_Utils_System::isNull($date)) { |
710 | return FALSE; | |
711 | } | |
712 | return TRUE; | |
713 | } | |
714 | ||
e73e7ec8 TO |
715 | /** |
716 | * Translate a TTL to a concrete expiration time. | |
717 | * | |
718 | * @param NULL|int|DateInterval $ttl | |
719 | * @param int $default | |
720 | * The value to use if $ttl is not specified (NULL). | |
721 | * @return int | |
722 | * Timestamp (seconds since epoch). | |
723 | * @throws \CRM_Utils_Cache_InvalidArgumentException | |
724 | */ | |
725 | public static function convertCacheTtlToExpires($ttl, $default) { | |
726 | if ($ttl === NULL) { | |
727 | $ttl = $default; | |
728 | } | |
729 | ||
730 | if (is_int($ttl)) { | |
731 | return time() + $ttl; | |
732 | } | |
733 | elseif ($ttl instanceof DateInterval) { | |
734 | return date_add(new DateTime(), $ttl)->getTimestamp(); | |
735 | } | |
736 | else { | |
737 | throw new CRM_Utils_Cache_InvalidArgumentException("Invalid cache TTL"); | |
738 | } | |
739 | } | |
740 | ||
741 | /** | |
742 | * Normalize a TTL. | |
743 | * | |
744 | * @param NULL|int|DateInterval $ttl | |
745 | * @param int $default | |
746 | * The value to use if $ttl is not specified (NULL). | |
747 | * @return int | |
748 | * Seconds until expiration. | |
749 | * @throws \CRM_Utils_Cache_InvalidArgumentException | |
750 | */ | |
751 | public static function convertCacheTtl($ttl, $default) { | |
752 | if ($ttl === NULL) { | |
753 | return $default; | |
754 | } | |
755 | elseif (is_int($ttl)) { | |
756 | return $ttl; | |
757 | } | |
758 | elseif ($ttl instanceof DateInterval) { | |
759 | return date_add(new DateTime(), $ttl)->getTimestamp() - time(); | |
760 | } | |
761 | else { | |
762 | throw new CRM_Utils_Cache_InvalidArgumentException("Invalid cache TTL"); | |
763 | } | |
764 | } | |
765 | ||
5bc392e6 EM |
766 | /** |
767 | * @param null $timeStamp | |
768 | * | |
769 | * @return bool|string | |
770 | */ | |
00be9182 | 771 | public static function currentDBDate($timeStamp = NULL) { |
6a488035 TO |
772 | return $timeStamp ? date('YmdHis', $timeStamp) : date('YmdHis'); |
773 | } | |
774 | ||
5bc392e6 EM |
775 | /** |
776 | * @param $date | |
777 | * @param null $now | |
778 | * | |
779 | * @return bool | |
780 | */ | |
00be9182 | 781 | public static function overdue($date, $now = NULL) { |
6a488035 TO |
782 | $mysqlDate = self::isoToMysql($date); |
783 | if (!$now) { | |
784 | $now = self::currentDBDate(); | |
785 | } | |
786 | else { | |
787 | $now = self::isoToMysql($now); | |
788 | } | |
789 | ||
ff25152a | 790 | return (strtotime($mysqlDate) >= strtotime($now)) ? FALSE : TRUE; |
6a488035 TO |
791 | } |
792 | ||
793 | /** | |
fe482240 | 794 | * Get customized today. |
6a488035 TO |
795 | * |
796 | * This function is used for getting customized today. To get | |
797 | * actuall today pass 'dayParams' as null. or else pass the day, | |
798 | * month, year values as array values | |
799 | * Example: $dayParams = array( | |
353ffa53 | 800 | * 'day' => '25', 'month' => '10', |
6a488035 TO |
801 | * 'year' => '2007' ); |
802 | * | |
d5cc0fc2 | 803 | * @param array $dayParams of the day, month, year. |
77855840 | 804 | * Array of the day, month, year. |
6a488035 | 805 | * values. |
77855840 TO |
806 | * @param string $format |
807 | * Expected date format( default. | |
6a488035 TO |
808 | * format is 2007-12-21 ) |
809 | * | |
a6c01b45 | 810 | * @return string |
b44e3f84 | 811 | * Return the customized today's date (Y-m-d) |
6a488035 | 812 | */ |
00be9182 | 813 | public static function getToday($dayParams = NULL, $format = "Y-m-d") { |
6a488035 TO |
814 | if (is_null($dayParams) || empty($dayParams)) { |
815 | $today = date($format); | |
816 | } | |
817 | else { | |
818 | $today = date($format, mktime(0, 0, 0, | |
353ffa53 TO |
819 | $dayParams['month'], |
820 | $dayParams['day'], | |
821 | $dayParams['year'] | |
822 | )); | |
6a488035 TO |
823 | } |
824 | ||
825 | return $today; | |
826 | } | |
827 | ||
828 | /** | |
100fef9d | 829 | * Find whether today's date lies in |
6a488035 TO |
830 | * the given range |
831 | * | |
77855840 TO |
832 | * @param date $startDate |
833 | * Start date for the range. | |
834 | * @param date $endDate | |
835 | * End date for the range. | |
6a488035 | 836 | * |
72b3a70c CW |
837 | * @return bool |
838 | * true if today's date is in the given date range | |
6a488035 | 839 | */ |
00be9182 | 840 | public static function getRange($startDate, $endDate) { |
353ffa53 | 841 | $today = date("Y-m-d"); |
6a488035 | 842 | $mysqlStartDate = self::isoToMysql($startDate); |
353ffa53 TO |
843 | $mysqlEndDate = self::isoToMysql($endDate); |
844 | $mysqlToday = self::isoToMysql($today); | |
6a488035 TO |
845 | |
846 | if ((isset($mysqlStartDate) && isset($mysqlEndDate)) && (($mysqlToday >= $mysqlStartDate) && ($mysqlToday <= $mysqlEndDate))) { | |
847 | return TRUE; | |
848 | } | |
849 | elseif ((isset($mysqlStartDate) && !isset($mysqlEndDate)) && (($mysqlToday >= $mysqlStartDate))) { | |
850 | return TRUE; | |
851 | } | |
852 | elseif ((!isset($mysqlStartDate) && isset($mysqlEndDate)) && (($mysqlToday <= $mysqlEndDate))) { | |
853 | return TRUE; | |
854 | } | |
855 | return FALSE; | |
856 | } | |
857 | ||
858 | /** | |
100fef9d | 859 | * Get start date and end from |
6a488035 TO |
860 | * the given relative term and unit |
861 | * | |
7ec58d2b J |
862 | * @param string $relative Relative format in the format term.unit. |
863 | * Eg: previous.day | |
f4aaa82a | 864 | * |
819e6707 J |
865 | * @param string $from |
866 | * @param string $to | |
867 | * @param string $fromTime | |
868 | * @param string $toTime | |
6a488035 | 869 | * |
a6c01b45 CW |
870 | * @return array |
871 | * start date, end date | |
6a488035 | 872 | */ |
819e6707 | 873 | public static function getFromTo($relative, $from, $to, $fromTime = NULL, $toTime = '235959') { |
6a488035 | 874 | if ($relative) { |
819e6707 | 875 | list($term, $unit) = explode('.', $relative, 2); |
6a488035 | 876 | $dateRange = self::relativeToAbsolute($term, $unit); |
819e6707 | 877 | $from = substr($dateRange['from'], 0, 8); |
6a488035 | 878 | $to = substr($dateRange['to'], 0, 8); |
7ec58d2b J |
879 | // @todo fix relativeToAbsolute & add tests |
880 | // relativeToAbsolute returns 8 char date strings | |
881 | // or 14 char date + time strings. | |
882 | // We should use those. However, it turns out to be unreliable. | |
883 | // e.g. this.week does NOT return 235959 for 'from' | |
884 | // so our defaults are more reliable. | |
885 | // Currently relativeToAbsolute only supports 'whole' days so that is ok | |
6a488035 TO |
886 | } |
887 | ||
819e6707 J |
888 | $from = self::processDate($from, $fromTime); |
889 | $to = self::processDate($to, $toTime); | |
6a488035 | 890 | |
be2fb01f | 891 | return [$from, $to]; |
6a488035 TO |
892 | } |
893 | ||
894 | /** | |
fe482240 | 895 | * Calculate Age in Years if greater than one year else in months. |
6a488035 | 896 | * |
77855840 TO |
897 | * @param date $birthDate |
898 | * Birth Date. | |
6a488035 | 899 | * |
a6c01b45 CW |
900 | * @return int |
901 | * array $results contains years or months | |
6a488035 | 902 | */ |
6714d8d2 | 903 | public static function calculateAge($birthDate) { |
be2fb01f | 904 | $results = []; |
6a488035 TO |
905 | $formatedBirthDate = CRM_Utils_Date::customFormat($birthDate, '%Y-%m-%d'); |
906 | ||
353ffa53 TO |
907 | $bDate = explode('-', $formatedBirthDate); |
908 | $birthYear = $bDate[0]; | |
6a488035 | 909 | $birthMonth = $bDate[1]; |
353ffa53 TO |
910 | $birthDay = $bDate[2]; |
911 | $year_diff = date("Y") - $birthYear; | |
6a488035 TO |
912 | |
913 | // don't calculate age CRM-3143 | |
914 | if ($birthYear == '1902') { | |
915 | return $results; | |
916 | } | |
917 | ||
918 | switch ($year_diff) { | |
919 | case 1: | |
920 | $month = (12 - $birthMonth) + date("m"); | |
921 | if ($month < 12) { | |
922 | if (date("d") < $birthDay) { | |
923 | $month--; | |
924 | } | |
925 | $results['months'] = $month; | |
926 | } | |
927 | elseif ($month == 12 && (date("d") < $birthDay)) { | |
928 | $results['months'] = $month - 1; | |
929 | } | |
930 | else { | |
931 | $results['years'] = $year_diff; | |
932 | } | |
933 | break; | |
934 | ||
935 | case 0: | |
936 | $month = date("m") - $birthMonth; | |
937 | $results['months'] = $month; | |
938 | break; | |
939 | ||
940 | default: | |
941 | $results['years'] = $year_diff; | |
942 | if ((date("m") < $birthMonth) || (date("m") == $birthMonth) && (date("d") < $birthDay)) { | |
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)) { |
353ffa53 | 969 | $hour = CRM_Utils_Array::value('H', $date); |
6a488035 TO |
970 | $minute = CRM_Utils_Array::value('i', $date); |
971 | $second = CRM_Utils_Array::value('s', $date); | |
353ffa53 TO |
972 | $month = CRM_Utils_Array::value('M', $date); |
973 | $day = CRM_Utils_Array::value('d', $date); | |
974 | $year = CRM_Utils_Array::value('Y', $date); | |
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]; | |
1112 | $relativeTermSuffix = isset($relativeTermParts[1]) ? $relativeTermParts[1] : ''; | |
6a488035 TO |
1113 | |
1114 | switch ($unit) { | |
1115 | case 'year': | |
1116 | switch ($relativeTerm) { | |
1117 | case 'this': | |
1118 | $from['d'] = $from['M'] = 1; | |
353ffa53 TO |
1119 | $to['d'] = 31; |
1120 | $to['M'] = 12; | |
1121 | $to['Y'] = $from['Y'] = $now['year']; | |
6a488035 TO |
1122 | break; |
1123 | ||
1124 | case 'previous': | |
1125 | $from['M'] = $from['d'] = 1; | |
353ffa53 TO |
1126 | $to['d'] = 31; |
1127 | $to['M'] = 12; | |
1128 | $to['Y'] = $from['Y'] = $now['year'] - 1; | |
6a488035 TO |
1129 | break; |
1130 | ||
1131 | case 'previous_before': | |
1132 | $from['M'] = $from['d'] = 1; | |
353ffa53 TO |
1133 | $to['d'] = 31; |
1134 | $to['M'] = 12; | |
1135 | $to['Y'] = $from['Y'] = $now['year'] - 2; | |
6a488035 TO |
1136 | break; |
1137 | ||
1138 | case 'previous_2': | |
1139 | $from['M'] = $from['d'] = 1; | |
353ffa53 TO |
1140 | $to['d'] = 31; |
1141 | $to['M'] = 12; | |
6a488035 | 1142 | $from['Y'] = $now['year'] - 2; |
353ffa53 | 1143 | $to['Y'] = $now['year'] - 1; |
6a488035 TO |
1144 | break; |
1145 | ||
1146 | case 'earlier': | |
1147 | $to['d'] = 31; | |
1148 | $to['M'] = 12; | |
1149 | $to['Y'] = $now['year'] - 1; | |
1150 | unset($from); | |
1151 | break; | |
1152 | ||
1153 | case 'greater': | |
1154 | $from['M'] = $from['d'] = 1; | |
1155 | $from['Y'] = $now['year']; | |
1156 | unset($to); | |
1157 | break; | |
1158 | ||
52f15bd6 | 1159 | case 'greater_previous': |
1160 | $from['d'] = 31; | |
1161 | $from['M'] = 12; | |
1162 | $from['Y'] = $now['year'] - 1; | |
1163 | unset($to); | |
1164 | break; | |
1165 | ||
6a488035 TO |
1166 | case 'ending': |
1167 | $to['d'] = $now['mday']; | |
1168 | $to['M'] = $now['mon']; | |
1169 | $to['Y'] = $now['year']; | |
1170 | $to['H'] = 23; | |
1171 | $to['i'] = $to['s'] = 59; | |
353ffa53 TO |
1172 | $from = self::intervalAdd('year', -1, $to); |
1173 | $from = self::intervalAdd('second', 1, $from); | |
6a488035 | 1174 | break; |
e902edd1 | 1175 | |
1176 | case 'current': | |
1177 | $from['M'] = $from['d'] = 1; | |
1178 | $from['Y'] = $now['year']; | |
1179 | $to['H'] = 23; | |
1180 | $to['i'] = $to['s'] = 59; | |
1181 | $to['d'] = $now['mday']; | |
1182 | $to['M'] = $now['mon']; | |
1183 | $to['Y'] = $now['year']; | |
1184 | break; | |
e2c3163d | 1185 | |
1186 | case 'ending_2': | |
1187 | $to['d'] = $now['mday']; | |
1188 | $to['M'] = $now['mon']; | |
1189 | $to['Y'] = $now['year']; | |
1190 | $to['H'] = 23; | |
1191 | $to['i'] = $to['s'] = 59; | |
353ffa53 TO |
1192 | $from = self::intervalAdd('year', -2, $to); |
1193 | $from = self::intervalAdd('second', 1, $from); | |
e2c3163d | 1194 | break; |
1195 | ||
1196 | case 'ending_3': | |
1197 | $to['d'] = $now['mday']; | |
1198 | $to['M'] = $now['mon']; | |
1199 | $to['Y'] = $now['year']; | |
1200 | $to['H'] = 23; | |
1201 | $to['i'] = $to['s'] = 59; | |
353ffa53 TO |
1202 | $from = self::intervalAdd('year', -3, $to); |
1203 | $from = self::intervalAdd('second', 1, $from); | |
e2c3163d | 1204 | break; |
c5670e9e | 1205 | |
1206 | case 'less': | |
353ffa53 TO |
1207 | $to['d'] = 31; |
1208 | $to['M'] = 12; | |
1209 | $to['Y'] = $now['year']; | |
c5670e9e | 1210 | unset($from); |
1211 | break; | |
1212 | ||
1213 | case 'next': | |
1214 | $from['M'] = $from['d'] = 1; | |
353ffa53 TO |
1215 | $to['d'] = 31; |
1216 | $to['M'] = 12; | |
1217 | $to['Y'] = $from['Y'] = $now['year'] + 1; | |
c5670e9e | 1218 | break; |
1219 | ||
1220 | case 'starting': | |
1221 | $from['d'] = $now['mday']; | |
1222 | $from['M'] = $now['mon']; | |
1223 | $from['Y'] = $now['year']; | |
1224 | $to['d'] = $now['mday'] - 1; | |
1225 | $to['M'] = $now['mon']; | |
1226 | $to['Y'] = $now['year'] + 1; | |
1227 | break; | |
ad336d61 | 1228 | |
1229 | default: | |
1230 | if ($relativeTermPrefix === 'ending') { | |
1231 | $to['d'] = $now['mday']; | |
1232 | $to['M'] = $now['mon']; | |
1233 | $to['Y'] = $now['year']; | |
1234 | $to['H'] = 23; | |
1235 | $to['i'] = $to['s'] = 59; | |
1236 | $from = self::intervalAdd('year', -$relativeTermSuffix, $to); | |
1237 | $from = self::intervalAdd('second', 1, $from); | |
1238 | } | |
6a488035 TO |
1239 | } |
1240 | break; | |
1241 | ||
1242 | case 'fiscal_year': | |
353ffa53 | 1243 | $config = CRM_Core_Config::singleton(); |
6a488035 TO |
1244 | $from['d'] = $config->fiscalYearStart['d']; |
1245 | $from['M'] = $config->fiscalYearStart['M']; | |
353ffa53 | 1246 | $fYear = self::calculateFiscalYear($from['d'], $from['M']); |
1e60a380 | 1247 | switch ($relativeTermPrefix) { |
6a488035 | 1248 | case 'this': |
353ffa53 | 1249 | $from['Y'] = $fYear; |
5eb3092f | 1250 | $fiscalYear = mktime(0, 0, 0, $from['M'], $from['d'] - 1, $from['Y'] + 1); |
353ffa53 | 1251 | $fiscalEnd = explode('-', date("Y-m-d", $fiscalYear)); |
6a488035 TO |
1252 | |
1253 | $to['d'] = $fiscalEnd['2']; | |
1254 | $to['M'] = $fiscalEnd['1']; | |
1255 | $to['Y'] = $fiscalEnd['0']; | |
1256 | break; | |
1257 | ||
1258 | case 'previous': | |
1e60a380 | 1259 | if (!is_numeric($relativeTermSuffix)) { |
1260 | $from['Y'] = ($relativeTermSuffix === 'before') ? $fYear - 2 : $fYear - 1; | |
1261 | $fiscalYear = mktime(0, 0, 0, $from['M'], $from['d'] - 1, $from['Y'] + 1); | |
1262 | $fiscalEnd = explode('-', date("Y-m-d", $fiscalYear)); | |
1263 | $to['d'] = $fiscalEnd['2']; | |
1264 | $to['M'] = $fiscalEnd['1']; | |
1265 | $to['Y'] = $fiscalEnd['0']; | |
1266 | } | |
1267 | else { | |
1268 | $from['Y'] = $fYear - $relativeTermSuffix; | |
1269 | $fiscalYear = mktime(0, 0, 0, $from['M'], $from['d'] - 1, $from['Y'] + 1); | |
1270 | $fiscalEnd = explode('-', date("Y-m-d", $fiscalYear)); | |
1271 | $to['d'] = $fiscalEnd['2']; | |
1272 | $to['M'] = $fiscalEnd['1']; | |
1273 | $to['Y'] = $fYear; | |
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; | |
1552 | $from['M'] = $now['mon'];; | |
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; | |
1594 | $from['M'] = $now['mon'];; | |
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']; | |
1830 | $from['M'] = $now['mon'];; | |
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 | ||
be2fb01f | 1859 | foreach ([ |
6714d8d2 SL |
1860 | 'from', |
1861 | 'to', | |
1862 | ] as $item) { | |
6a488035 TO |
1863 | if (!empty($$item)) { |
1864 | $dateRange[$item] = self::format($$item); | |
1865 | } | |
1866 | else { | |
1867 | $dateRange[$item] = NULL; | |
1868 | } | |
1869 | } | |
1870 | return $dateRange; | |
1871 | } | |
1872 | ||
1873 | /** | |
fe482240 | 1874 | * Calculate current fiscal year based on the fiscal month and day. |
6a488035 | 1875 | * |
77855840 TO |
1876 | * @param int $fyDate |
1877 | * Fiscal start date. | |
6a488035 | 1878 | * |
77855840 TO |
1879 | * @param int $fyMonth |
1880 | * Fiscal Start Month. | |
6a488035 | 1881 | * |
a6c01b45 | 1882 | * @return int |
1e60a380 | 1883 | * $fy Current Fiscal Year |
6a488035 | 1884 | */ |
00be9182 | 1885 | public static function calculateFiscalYear($fyDate, $fyMonth) { |
6a488035 TO |
1886 | $date = date("Y-m-d"); |
1887 | $currentYear = date("Y"); | |
1888 | ||
50bfb460 | 1889 | // recalculate the date because month 4::04 make the difference |
353ffa53 TO |
1890 | $fiscalYear = explode('-', date("Y-m-d", mktime(0, 0, 0, $fyMonth, $fyDate, $currentYear))); |
1891 | $fyDate = $fiscalYear[2]; | |
1892 | $fyMonth = $fiscalYear[1]; | |
6a488035 TO |
1893 | $fyStartDate = date("Y-m-d", mktime(0, 0, 0, $fyMonth, $fyDate, $currentYear)); |
1894 | ||
1895 | if ($fyStartDate > $date) { | |
1896 | $fy = intval(intval($currentYear) - 1); | |
1897 | } | |
1898 | else { | |
1899 | $fy = intval($currentYear); | |
1900 | } | |
1901 | return $fy; | |
1902 | } | |
1903 | ||
1904 | /** | |
50bfb460 | 1905 | * Function to process date, convert to mysql format |
6a488035 | 1906 | * |
77855840 TO |
1907 | * @param string $date |
1908 | * Date string. | |
1909 | * @param string $time | |
1910 | * Time string. | |
f4aaa82a | 1911 | * @param bool|string $returnNullString 'null' needs to be returned |
6a488035 | 1912 | * so that db oject will set null in db |
77855840 TO |
1913 | * @param string $format |
1914 | * Expected return date format.( default is mysql ). | |
6a488035 | 1915 | * |
a6c01b45 CW |
1916 | * @return string |
1917 | * date format that is excepted by mysql | |
6a488035 | 1918 | */ |
00be9182 | 1919 | public static function processDate($date, $time = NULL, $returnNullString = FALSE, $format = 'YmdHis') { |
6a488035 TO |
1920 | $mysqlDate = NULL; |
1921 | ||
1922 | if ($returnNullString) { | |
1923 | $mysqlDate = 'null'; | |
1924 | } | |
1925 | ||
1926 | if (trim($date)) { | |
1927 | $mysqlDate = date($format, strtotime($date . ' ' . $time)); | |
1928 | } | |
1929 | ||
1930 | return $mysqlDate; | |
1931 | } | |
1932 | ||
d86e674f | 1933 | /** |
1934 | * Add the metadata about a date field to the field. | |
1935 | * | |
1936 | * This metadata will work with the call $form->add('datepicker', ... | |
1937 | * | |
1938 | * @param array $fieldMetaData | |
1939 | * @param array $field | |
1940 | * | |
1941 | * @return array | |
1942 | */ | |
1943 | public static function addDateMetadataToField($fieldMetaData, $field) { | |
1944 | if (isset($fieldMetaData['html'])) { | |
1945 | $field['html_type'] = $fieldMetaData['html']['type']; | |
e9f229db | 1946 | if ($field['html_type'] === 'Select Date') { |
1947 | if (!isset($field['date_format'])) { | |
1948 | $dateAttributes = CRM_Core_SelectValues::date($fieldMetaData['html']['formatType'], NULL, NULL, NULL, 'Input'); | |
1949 | $field['start_date_years'] = $dateAttributes['minYear']; | |
1950 | $field['end_date_years'] = $dateAttributes['maxYear']; | |
1951 | $field['date_format'] = $dateAttributes['format']; | |
1952 | $field['is_datetime_field'] = TRUE; | |
1953 | $field['time_format'] = $dateAttributes['time']; | |
ed0ca248 | 1954 | $field['smarty_view_format'] = $dateAttributes['smarty_view_format']; |
d86e674f | 1955 | } |
e9f229db | 1956 | $field['datepicker']['extra'] = self::getDatePickerExtra($field); |
1957 | $field['datepicker']['attributes'] = self::getDatePickerAttributes($field); | |
d86e674f | 1958 | } |
d86e674f | 1959 | } |
1960 | return $field; | |
1961 | } | |
1962 | ||
d86e674f | 1963 | /** |
1964 | * Get the fields required for the 'extra' parameter when adding a datepicker. | |
1965 | * | |
1966 | * @param array $field | |
1967 | * | |
1968 | * @return array | |
1969 | */ | |
1970 | public static function getDatePickerExtra($field) { | |
be2fb01f | 1971 | $extra = []; |
d86e674f | 1972 | if (isset($field['date_format'])) { |
1973 | $extra['date'] = $field['date_format']; | |
1974 | $extra['time'] = $field['time_format']; | |
1975 | } | |
1976 | $thisYear = date('Y'); | |
1977 | if (isset($field['start_date_years'])) { | |
1978 | $extra['minDate'] = date('Y-m-d', strtotime('-' . ($thisYear - $field['start_date_years']) . ' years')); | |
1979 | } | |
1980 | if (isset($field['end_date_years'])) { | |
1981 | $extra['maxDate'] = date('Y-m-d', strtotime('-' . ($thisYear - $field['end_date_years']) . ' years')); | |
1982 | } | |
1983 | return $extra; | |
1984 | } | |
1985 | ||
1986 | /** | |
1987 | * Get the attributes parameters required for datepicker. | |
1988 | * | |
1989 | * @param array $field | |
1990 | * Field metadata | |
1991 | * | |
1992 | * @return array | |
1993 | * Array ready to pass to $this->addForm('datepicker' as attributes. | |
1994 | */ | |
1995 | public static function getDatePickerAttributes(&$field) { | |
be2fb01f CW |
1996 | $attributes = []; |
1997 | $dateAttributes = [ | |
d86e674f | 1998 | 'start_date_years' => 'minYear', |
1999 | 'end_date_years' => 'maxYear', | |
2000 | 'date_format' => 'format', | |
be2fb01f | 2001 | ]; |
d86e674f | 2002 | foreach ($dateAttributes as $dateAttribute => $mapTo) { |
2003 | if (isset($field[$dateAttribute])) { | |
2004 | $attributes[$mapTo] = $field[$dateAttribute]; | |
2005 | } | |
2006 | } | |
2007 | return $attributes; | |
2008 | } | |
2009 | ||
6a488035 | 2010 | /** |
50bfb460 | 2011 | * Function to convert mysql to date plugin format. |
6a488035 | 2012 | * |
77855840 TO |
2013 | * @param string $mysqlDate |
2014 | * Date string. | |
f4aaa82a EM |
2015 | * |
2016 | * @param null $formatType | |
2017 | * @param null $format | |
2018 | * @param null $timeFormat | |
6a488035 | 2019 | * |
a6c01b45 CW |
2020 | * @return array |
2021 | * and time | |
6a488035 | 2022 | */ |
00be9182 | 2023 | public static function setDateDefaults($mysqlDate = NULL, $formatType = NULL, $format = NULL, $timeFormat = NULL) { |
6a488035 TO |
2024 | // if date is not passed assume it as today |
2025 | if (!$mysqlDate) { | |
2026 | $mysqlDate = date('Y-m-d G:i:s'); | |
2027 | } | |
2028 | ||
2029 | $config = CRM_Core_Config::singleton(); | |
2030 | if ($formatType) { | |
2031 | // get actual format | |
be2fb01f CW |
2032 | $params = ['name' => $formatType]; |
2033 | $values = []; | |
6a488035 TO |
2034 | CRM_Core_DAO::commonRetrieve('CRM_Core_DAO_PreferencesDate', $params, $values); |
2035 | ||
2036 | if ($values['date_format']) { | |
2037 | $format = $values['date_format']; | |
2038 | } | |
2039 | ||
2040 | if (isset($values['time_format'])) { | |
2041 | $timeFormat = $values['time_format']; | |
2042 | } | |
2043 | } | |
2044 | ||
2045 | // now we set display date using js, hence we should always setdefault | |
2046 | // 'm/d/Y' format. So that submitted value is alwats mm/dd/YY format | |
2047 | // note that for date display we dynamically create text field | |
2048 | /* | |
e70a7fc0 TO |
2049 | if ( !$format ) { |
2050 | $format = $config->dateInputFormat; | |
2051 | } | |
6a488035 | 2052 | |
e70a7fc0 TO |
2053 | // get actual format |
2054 | $actualPHPFormats = CRM_Core_SelectValues::datePluginToPHPFormats( ); | |
2055 | $dateFormat = CRM_Utils_Array::value( $format, $actualPHPFormats ); | |
2056 | */ | |
6a488035 | 2057 | |
6a488035 TO |
2058 | $dateFormat = 'm/d/Y'; |
2059 | $date = date($dateFormat, strtotime($mysqlDate)); | |
2060 | ||
2061 | if (!$timeFormat) { | |
2062 | $timeFormat = $config->timeInputFormat; | |
2063 | } | |
2064 | ||
2065 | $actualTimeFormat = "g:iA"; | |
2066 | $appendZeroLength = 7; | |
2067 | if ($timeFormat > 1) { | |
2068 | $actualTimeFormat = "G:i"; | |
2069 | $appendZeroLength = 5; | |
2070 | } | |
2071 | ||
2072 | $time = date($actualTimeFormat, strtotime($mysqlDate)); | |
2073 | ||
2074 | // need to append zero for hours < 10 | |
2075 | if (strlen($time) < $appendZeroLength) { | |
2076 | $time = '0' . $time; | |
2077 | } | |
2078 | ||
be2fb01f | 2079 | return [$date, $time]; |
6a488035 TO |
2080 | } |
2081 | ||
2082 | /** | |
fe482240 | 2083 | * Function get date format. |
6a488035 | 2084 | * |
77855840 TO |
2085 | * @param string $formatType |
2086 | * Date name e.g. birth. | |
6a488035 | 2087 | * |
a6c01b45 | 2088 | * @return string |
6a488035 | 2089 | */ |
00be9182 | 2090 | public static function getDateFormat($formatType = NULL) { |
6a488035 TO |
2091 | $format = NULL; |
2092 | if ($formatType) { | |
395d8dc6 | 2093 | $format = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_PreferencesDate', |
353ffa53 | 2094 | $formatType, 'date_format', 'name' |
6a488035 TO |
2095 | ); |
2096 | } | |
2097 | ||
2098 | if (!$format) { | |
2099 | $config = CRM_Core_Config::singleton(); | |
2100 | $format = $config->dateInputFormat; | |
2101 | } | |
2102 | return $format; | |
2103 | } | |
2104 | ||
5bc392e6 EM |
2105 | /** |
2106 | * @param $date | |
2107 | * @param $dateType | |
2108 | * | |
2109 | * @return null|string | |
2110 | */ | |
00be9182 | 2111 | public static function formatDate($date, $dateType) { |
6a488035 TO |
2112 | $formattedDate = NULL; |
2113 | if (empty($date)) { | |
2114 | return $formattedDate; | |
2115 | } | |
2116 | ||
50bfb460 SB |
2117 | // 1. first convert date to default format. |
2118 | // 2. append time to default formatted date (might be removed during format) | |
2119 | // 3. validate date / date time. | |
2120 | // 4. If date and time then convert to default date time format. | |
6a488035 TO |
2121 | |
2122 | $dateKey = 'date'; | |
be2fb01f | 2123 | $dateParams = [$dateKey => $date]; |
6a488035 TO |
2124 | |
2125 | if (CRM_Utils_Date::convertToDefaultDate($dateParams, $dateType, $dateKey)) { | |
2126 | $dateVal = $dateParams[$dateKey]; | |
2127 | $ruleName = 'date'; | |
2128 | if ($dateType == 1) { | |
be2fb01f | 2129 | $matches = []; |
6a488035 TO |
2130 | if (preg_match("/(\s(([01]\d)|[2][0-3]):([0-5]\d))$/", $date, $matches)) { |
2131 | $ruleName = 'dateTime'; | |
2132 | if (strpos($date, '-') !== FALSE) { | |
2133 | $dateVal .= array_shift($matches); | |
2134 | } | |
2135 | } | |
2136 | } | |
2137 | ||
2138 | // validate date. | |
0e6e8724 | 2139 | $valid = CRM_Utils_Rule::$ruleName($dateVal); |
6a488035 TO |
2140 | |
2141 | if ($valid) { | |
50bfb460 | 2142 | // format date and time to default. |
6a488035 TO |
2143 | if ($ruleName == 'dateTime') { |
2144 | $dateVal = CRM_Utils_Date::customFormat(preg_replace("/(:|\s)?/", "", $dateVal), '%Y%m%d%H%i'); | |
50bfb460 | 2145 | // hack to add seconds |
6a488035 TO |
2146 | $dateVal .= '00'; |
2147 | } | |
2148 | $formattedDate = $dateVal; | |
2149 | } | |
2150 | } | |
2151 | ||
2152 | return $formattedDate; | |
2153 | } | |
2154 | ||
dccd9f4f ERL |
2155 | /** |
2156 | * Function to return days of the month. | |
2157 | * | |
2158 | * @return array | |
2159 | */ | |
2160 | public static function getCalendarDayOfMonth() { | |
be2fb01f | 2161 | $month = []; |
dccd9f4f ERL |
2162 | for ($i = 1; $i <= 31; $i++) { |
2163 | $month[$i] = $i; | |
2164 | if ($i == 31) { | |
2165 | $month[$i] = $i . ' / Last day of month'; | |
2166 | } | |
2167 | } | |
2168 | return $month; | |
2169 | } | |
2170 | ||
6e793248 | 2171 | /** |
2172 | * Convert a relative date format to an api field. | |
2173 | * | |
2174 | * @param array $params | |
2175 | * @param string $dateField | |
2176 | * @param bool $isDatePicker | |
2177 | * Non datepicker fields are deprecated. Exterminate Exterminate. | |
2178 | * (but for now handle them). | |
2179 | */ | |
2180 | public static function convertFormDateToApiFormat(&$params, $dateField, $isDatePicker = TRUE) { | |
2181 | if (!empty($params[$dateField . '_relative'])) { | |
2182 | $dates = CRM_Utils_Date::getFromTo($params[$dateField . '_relative'], NULL, NULL); | |
2183 | unset($params[$dateField . '_relative']); | |
2184 | } | |
2185 | if (!empty($params[$dateField . '_low'])) { | |
2186 | $dates[0] = $isDatePicker ? $params[$dateField . '_low'] : date('Y-m-d H:i:s', strtotime($params[$dateField . '_low'])); | |
2187 | unset($params[$dateField . '_low']); | |
2188 | } | |
2189 | if (!empty($params[$dateField . '_high'])) { | |
2190 | $dates[1] = $isDatePicker ? $params[$dateField . '_high'] : date('Y-m-d 23:59:59', strtotime($params[$dateField . '_high'])); | |
2191 | unset($params[$dateField . '_high']); | |
2192 | } | |
2193 | if (empty($dates)) { | |
2194 | return; | |
2195 | } | |
2196 | if (empty($dates[0])) { | |
2197 | $params[$dateField] = ['<=' => $dates[1]]; | |
2198 | } | |
2199 | elseif (empty($dates[1])) { | |
2200 | $params[$dateField] = ['>=' => $dates[0]]; | |
2201 | } | |
2202 | else { | |
2203 | $params[$dateField] = ['BETWEEN' => $dates]; | |
2204 | } | |
2205 | } | |
2206 | ||
6a488035 | 2207 | } |