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