Merge pull request #4759 from totten/master-civimail-status
[civicrm-core.git] / CRM / Utils / Date.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
06b69b18 4 | CiviCRM version 4.5 |
6a488035 5 +--------------------------------------------------------------------+
06b69b18 6 | Copyright CiviCRM LLC (c) 2004-2014 |
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 +--------------------------------------------------------------------+
26*/
27
28/**
29 *
30 * @package CRM
06b69b18 31 * @copyright CiviCRM LLC (c) 2004-2014
6a488035
TO
32 * $Id$
33 *
34 */
35
36/**
37 * Date utilties
38 */
39class CRM_Utils_Date {
40
41 /**
42 * format a date by padding it with leading '0'.
43 *
f4aaa82a
EM
44 * @param array $date ('Y', 'M', 'd')
45 * @param string $separator the seperator to use when formatting the date
46 * @param int|string $invalidDate what to return if the date is invalid
6a488035
TO
47 *
48 * @return string - formatted string for date
49 *
50 * @static
51 */
52 static function format($date, $separator = '', $invalidDate = 0) {
53 if (is_numeric($date) &&
54 ((strlen($date) == 8) || (strlen($date) == 14))
55 ) {
56 return $date;
57 }
58
59 if (!is_array($date) ||
60 CRM_Utils_System::isNull($date) ||
61 empty($date['Y'])
62 ) {
63 return $invalidDate;
64 }
65
66 $date['Y'] = (int ) $date['Y'];
67 if ($date['Y'] < 1000 || $date['Y'] > 2999) {
68 return $invalidDate;
69 }
70
71 if (array_key_exists('m', $date)) {
72 $date['M'] = $date['m'];
73 }
74 elseif (array_key_exists('F', $date)) {
75 $date['M'] = $date['F'];
76 }
77
a7488080 78 if (!empty($date['M'])) {
6a488035
TO
79 $date['M'] = (int ) $date['M'];
80 if ($date['M'] < 1 || $date['M'] > 12) {
81 return $invalidDate;
82 }
83 }
84 else {
85 $date['M'] = 1;
86 }
87
a7488080 88 if (!empty($date['d'])) {
6a488035
TO
89 $date['d'] = (int ) $date['d'];
90 }
91 else {
92 $date['d'] = 1;
93 }
94
95 if (!checkdate($date['M'], $date['d'], $date['Y'])) {
96 return $invalidDate;
97 }
98
99 $date['M'] = sprintf('%02d', $date['M']);
100 $date['d'] = sprintf('%02d', $date['d']);
101
102 $time = '';
103 if (CRM_Utils_Array::value('H', $date) != NULL ||
104 CRM_Utils_Array::value('h', $date) != NULL ||
105 CRM_Utils_Array::value('i', $date) != NULL ||
106 CRM_Utils_Array::value('s', $date) != NULL
107 ) {
108 // we have time too..
a7488080 109 if (!empty($date['h'])) {
6a488035
TO
110 if (CRM_Utils_Array::value('A', $date) == 'PM' or CRM_Utils_Array::value('a', $date) == 'pm') {
111 if ($date['h'] != 12) {
112 $date['h'] = $date['h'] + 12;
113 }
114 }
115 if ((CRM_Utils_Array::value('A', $date) == 'AM' or CRM_Utils_Array::value('a', $date) == 'am') &&
116 CRM_Utils_Array::value('h', $date) == 12
117 ) {
118 $date['h'] = '00';
119 }
120
121 $date['h'] = (int ) $date['h'];
122 }
123 else {
124 $date['h'] = 0;
125 }
126
127 // in 24-hour format the hour is under the 'H' key
a7488080 128 if (!empty($date['H'])) {
6a488035
TO
129 $date['H'] = (int) $date['H'];
130 }
131 else {
132 $date['H'] = 0;
133 }
134
a7488080 135 if (!empty($date['i'])) {
6a488035
TO
136 $date['i'] = (int ) $date['i'];
137 }
138 else {
139 $date['i'] = 0;
140 }
141
142 if ($date['h'] == 0 && $date['H'] != 0) {
143 $date['h'] = $date['H'];
144 }
145
a7488080 146 if (!empty($date['s'])) {
6a488035
TO
147 $date['s'] = (int ) $date['s'];
148 }
149 else {
150 $date['s'] = 0;
151 }
152
153 $date['h'] = sprintf('%02d', $date['h']);
154 $date['i'] = sprintf('%02d', $date['i']);
155 $date['s'] = sprintf('%02d', $date['s']);
156
157 if ($separator) {
158 $time = '&nbsp;';
159 }
160 $time .= $date['h'] . $separator . $date['i'] . $separator . $date['s'];
161 }
162
163 return $date['Y'] . $separator . $date['M'] . $separator . $date['d'] . $time;
164 }
165
166 /**
167 * return abbreviated weekday names according to the locale
168 *
169 * @return array 0-based array with abbreviated weekday names
170 *
171 * @static
172 */
173 static function &getAbbrWeekdayNames() {
174 static $abbrWeekdayNames;
175 if (!isset($abbrWeekdayNames)) {
176
177 // set LC_TIME and build the arrays from locale-provided names
178 // June 1st, 1970 was a Monday
179 CRM_Core_I18n::setLcTime();
180 for ($i = 0; $i < 7; $i++) {
181 $abbrWeekdayNames[$i] = strftime('%a', mktime(0, 0, 0, 6, $i, 1970));
182 }
183 }
184 return $abbrWeekdayNames;
185 }
186
187 /**
188 * return full weekday names according to the locale
189 *
190 * @return array 0-based array with full weekday names
191 *
192 * @static
193 */
194 static function &getFullWeekdayNames() {
195 static $fullWeekdayNames;
196 if (!isset($fullWeekdayNames)) {
197
198 // set LC_TIME and build the arrays from locale-provided names
199 // June 1st, 1970 was a Monday
200 CRM_Core_I18n::setLcTime();
201 for ($i = 0; $i < 7; $i++) {
202 $fullWeekdayNames[$i] = strftime('%A', mktime(0, 0, 0, 6, $i, 1970));
203 }
204 }
205 return $fullWeekdayNames;
206 }
207
208 /**
209 * return abbreviated month names according to the locale
210 *
f4aaa82a
EM
211 * @param bool $month
212 *
6a488035
TO
213 * @return array 1-based array with abbreviated month names
214 *
215 * @static
216 */
217 static function &getAbbrMonthNames($month = FALSE) {
218 static $abbrMonthNames;
219 if (!isset($abbrMonthNames)) {
220
221 // set LC_TIME and build the arrays from locale-provided names
222 CRM_Core_I18n::setLcTime();
223 for ($i = 1; $i <= 12; $i++) {
224 $abbrMonthNames[$i] = strftime('%b', mktime(0, 0, 0, $i, 10, 1970));
225 }
226 }
227 if ($month) {
228 return $abbrMonthNames[$month];
229 }
230 return $abbrMonthNames;
231 }
232
233 /**
234 * return full month names according to the locale
235 *
236 * @return array 1-based array with full month names
237 *
238 * @static
239 */
240 static function &getFullMonthNames() {
241 static $fullMonthNames;
242 if (!isset($fullMonthNames)) {
243
244 // set LC_TIME and build the arrays from locale-provided names
245 CRM_Core_I18n::setLcTime();
246 for ($i = 1; $i <= 12; $i++) {
247 $fullMonthNames[$i] = strftime('%B', mktime(0, 0, 0, $i, 10, 1970));
248 }
249 }
250 return $fullMonthNames;
251 }
252
5bc392e6
EM
253 /**
254 * @param $string
255 *
256 * @return int
257 */
6a488035
TO
258 static function unixTime($string) {
259 if (empty($string)) {
260 return 0;
261 }
262 $parsedDate = date_parse($string);
263 return mktime(CRM_Utils_Array::value('hour', $parsedDate),
264 CRM_Utils_Array::value('minute', $parsedDate),
265 59,
266 CRM_Utils_Array::value('month', $parsedDate),
267 CRM_Utils_Array::value('day', $parsedDate),
268 CRM_Utils_Array::value('year', $parsedDate)
269 );
270 }
271
272 /**
273 * create a date and time string in a provided format
274 *
275 * %b - abbreviated month name ('Jan'..'Dec')
276 * %B - full month name ('January'..'December')
277 * %d - day of the month as a decimal number, 0-padded ('01'..'31')
278 * %e - day of the month as a decimal number, blank-padded (' 1'..'31')
279 * %E - day of the month as a decimal number ('1'..'31')
280 * %f - English ordinal suffix for the day of the month ('st', 'nd', 'rd', 'th')
281 * %H - hour in 24-hour format, 0-padded ('00'..'23')
282 * %I - hour in 12-hour format, 0-padded ('01'..'12')
283 * %k - hour in 24-hour format, blank-padded (' 0'..'23')
284 * %l - hour in 12-hour format, blank-padded (' 1'..'12')
285 * %m - month as a decimal number, 0-padded ('01'..'12')
286 * %M - minute, 0-padded ('00'..'60')
287 * %p - lowercase ante/post meridiem ('am', 'pm')
288 * %P - uppercase ante/post meridiem ('AM', 'PM')
289 * %Y - year as a decimal number including the century ('2005')
290 *
f5d25ee9 291 * @param string $dateString
f4aaa82a
EM
292 * @param string $format the output format
293 * @param array $dateParts an array with the desired date parts
6a488035 294 *
f4aaa82a 295 * @internal param string $date date and time in 'YYYY-MM-DD hh:mm:ss' format
6a488035
TO
296 * @return string the $format-formatted $date
297 *
298 * @static
299 */
300 static function customFormat($dateString, $format = NULL, $dateParts = NULL) {
301 // 1-based (January) month names arrays
302 $abbrMonths = self::getAbbrMonthNames();
303 $fullMonths = self::getFullMonthNames();
304
305 if (!$format) {
306 $config = CRM_Core_Config::singleton();
307
308 if ($dateParts) {
309 if (array_intersect(array('h', 'H'), $dateParts)) {
310 $format = $config->dateformatDatetime;
311 }
312 elseif (array_intersect(array('d', 'j'), $dateParts)) {
313 $format = $config->dateformatFull;
314 }
315 elseif (array_intersect(array('m', 'M'), $dateParts)) {
316 $format = $config->dateformatPartial;
317 }
318 else {
319 $format = $config->dateformatYear;
320 }
321 }
322 else {
323 if (strpos($dateString, '-')) {
324 $month = (int) substr($dateString, 5, 2);
325 $day = (int) substr($dateString, 8, 2);
326 }
327 else {
328 $month = (int) substr($dateString, 4, 2);
329 $day = (int) substr($dateString, 6, 2);
330 }
331
332 if (strlen($dateString) > 10) {
333 $format = $config->dateformatDatetime;
334 }
335 elseif ($day > 0) {
336 $format = $config->dateformatFull;
337 }
338 elseif ($month > 0) {
339 $format = $config->dateformatPartial;
340 }
341 else {
342 $format = $config->dateformatYear;
343 }
344 }
345 }
346
347 if ($dateString) {
348 if (strpos($dateString, '-')) {
349 $year = (int) substr($dateString, 0, 4);
350 $month = (int) substr($dateString, 5, 2);
351 $day = (int) substr($dateString, 8, 2);
352
353 $hour24 = (int) substr($dateString, 11, 2);
354 $minute = (int) substr($dateString, 14, 2);
355 }
356 else {
357 $year = (int) substr($dateString, 0, 4);
358 $month = (int) substr($dateString, 4, 2);
359 $day = (int) substr($dateString, 6, 2);
360
361 $hour24 = (int) substr($dateString, 8, 2);
362 $minute = (int) substr($dateString, 10, 2);
363 }
364
365 if ($day % 10 == 1 and $day != 11) {
366 $suffix = 'st';
367 }
368 elseif ($day % 10 == 2 and $day != 12) {
369 $suffix = 'nd';
370 }
371 elseif ($day % 10 == 3 and $day != 13) {
372 $suffix = 'rd';
373 }
374 else {
375 $suffix = 'th';
376 }
377
378 if ($hour24 < 12) {
379 if ($hour24 == 00) {
380 $hour12 = 12;
381 }
382 else {
383 $hour12 = $hour24;
384 }
385 $type = 'AM';
386 }
387 else {
388 if ($hour24 == 12) {
389 $hour12 = 12;
390 }
391 else {
392 $hour12 = $hour24 - 12;
393 }
394 $type = 'PM';
395 }
396
397 $date = array(
398 '%b' => CRM_Utils_Array::value($month, $abbrMonths),
399 '%B' => CRM_Utils_Array::value($month, $fullMonths),
400 '%d' => $day > 9 ? $day : '0' . $day,
401 '%e' => $day > 9 ? $day : ' ' . $day,
402 '%E' => $day,
403 '%f' => $suffix,
404 '%H' => $hour24 > 9 ? $hour24 : '0' . $hour24,
405 '%h' => $hour12 > 9 ? $hour12 : '0' . $hour12,
406 '%I' => $hour12 > 9 ? $hour12 : '0' . $hour12,
407 '%k' => $hour24 > 9 ? $hour24 : ' ' . $hour24,
408 '%l' => $hour12 > 9 ? $hour12 : ' ' . $hour12,
409 '%m' => $month > 9 ? $month : '0' . $month,
410 '%M' => $minute > 9 ? $minute : '0' . $minute,
411 '%i' => $minute > 9 ? $minute : '0' . $minute,
412 '%p' => strtolower($type),
413 '%P' => $type,
414 '%A' => $type,
415 '%Y' => $year,
416 );
417
418 return strtr($format, $date);
419 }
420 else {
421 return '';
422 }
423 }
424
425 /**
426 * converts the date/datetime from MySQL format to ISO format
427 *
428 * @param string $mysql date/datetime in MySQL format
429 *
430 * @return string date/datetime in ISO format
431 * @static
432 */
433 static function mysqlToIso($mysql) {
434 $year = substr($mysql, 0, 4);
435 $month = substr($mysql, 4, 2);
436 $day = substr($mysql, 6, 2);
437 $hour = substr($mysql, 8, 2);
438 $minute = substr($mysql, 10, 2);
439 $second = substr($mysql, 12, 2);
440
441 $iso = '';
442 if ($year) {
443 $iso .= "$year";
444 }
445 if ($month) {
446 $iso .= "-$month";
447 if ($day) {
448 $iso .= "-$day";
449 }
450 }
451
452 if ($hour) {
453 $iso .= " $hour";
454 if ($minute) {
455 $iso .= ":$minute";
456 if ($second) {
457 $iso .= ":$second";
458 }
459 }
460 }
461 return $iso;
462 }
463
464 /**
465 * converts the date/datetime from ISO format to MySQL format
5d9f6898
EM
466 * Note that until CRM-14986/ 4.4.7 this was required whenever the pattern $dao->find(TRUE): $dao->save(); was
467 * used to update an object with a date field was used. The DAO now checks for a '-' in date field strings
468 * & runs this function if the - appears - meaning it is likely redundant in the form & BAO layers
6a488035
TO
469 *
470 * @param string $iso date/datetime in ISO format
471 *
472 * @return string date/datetime in MySQL format
473 * @static
474 */
475 static function isoToMysql($iso) {
476 $dropArray = array('-' => '', ':' => '', ' ' => '');
477 return strtr($iso, $dropArray);
478 }
479
480 /**
481 * converts the any given date to default date format.
482 *
f4aaa82a
EM
483 * @param array $params has given date-format
484 * @param int $dateType type of date
485 * @param string $dateParam index of params
486 *
487 * @return bool
6a488035
TO
488 * @static
489 */
d4c8a770 490 static function convertToDefaultDate(&$params, $dateType, $dateParam) {
6a488035
TO
491 $now = getDate();
492 $cen = substr($now['year'], 0, 2);
493 $prevCen = $cen - 1;
494
495 $value = NULL;
a7488080 496 if (!empty($params[$dateParam])) {
6a488035
TO
497 // suppress hh:mm or hh:mm:ss if it exists CRM-7957
498 $value = preg_replace("/(\s(([01]\d)|[2][0-3])(:([0-5]\d)){1,2})$/", "", $params[$dateParam]);
499 }
500
501 switch ($dateType) {
502 case 1:
503 if (!preg_match('/^\d\d\d\d-?(\d|\d\d)-?(\d|\d\d)$/', $value)) {
504 return FALSE;
505 }
506 break;
507
508 case 2:
509 if (!preg_match('/^(\d|\d\d)[-\/](\d|\d\d)[-\/]\d\d$/', $value)) {
510 return FALSE;
511 }
512 break;
513
514 case 4:
515 if (!preg_match('/^(\d|\d\d)[-\/](\d|\d\d)[-\/]\d\d\d\d$/', $value)) {
516 return FALSE;
517 }
518 break;
519
520 case 8:
521 if (!preg_match('/^[A-Za-z]*.[ \t]?\d\d\,[ \t]?\d\d\d\d$/', $value)) {
522 return FALSE;
523 }
524 break;
525
526 case 16:
527 if (!preg_match('/^\d\d-[A-Za-z]{3}.*-\d\d$/', $value) && !preg_match('/^\d\d[-\/]\d\d[-\/]\d\d$/', $value)) {
528 return FALSE;
529 }
530 break;
531
532 case 32:
533 if (!preg_match('/^(\d|\d\d)[-\/](\d|\d\d)[-\/]\d\d\d\d/', $value)) {
534 return FALSE;
535 }
536 break;
537 }
538
539 if ($dateType == 1) {
540 $formattedDate = explode("-", $value);
541 if (count($formattedDate) == 3) {
542 $year = (int) $formattedDate[0];
543 $month = (int) $formattedDate[1];
544 $day = (int) $formattedDate[2];
545 }
546 elseif (count($formattedDate) == 1 && (strlen($value) == 8)) {
547 return TRUE;
548 }
549 else {
550 return FALSE;
551 }
552 }
553
554
555 if ($dateType == 2 || $dateType == 4) {
556 $formattedDate = explode("/", $value);
557 if (count($formattedDate) != 3) {
558 $formattedDate = explode("-", $value);
559 }
560 if (count($formattedDate) == 3) {
561 $year = (int) $formattedDate[2];
562 $month = (int) $formattedDate[0];
563 $day = (int) $formattedDate[1];
564 }
565 else {
566 return FALSE;
567 }
568 }
569 if ($dateType == 8) {
570 $dateArray = explode(' ', $value);
571 // ignore comma(,)
572 $dateArray[1] = (int) substr($dateArray[1], 0, 2);
573
574 $monthInt = 0;
575 $fullMonths = self::getFullMonthNames();
576 foreach ($fullMonths as $key => $val) {
577 if (strtolower($dateArray[0]) == strtolower($val)) {
578 $monthInt = $key;
579 break;
580 }
581 }
582 if (!$monthInt) {
583 $abbrMonths = self::getAbbrMonthNames();
584 foreach ($abbrMonths as $key => $val) {
585 if (strtolower(trim($dateArray[0], ".")) == strtolower($val)) {
586 $monthInt = $key;
587 break;
588 }
589 }
590 }
591 $year = (int) $dateArray[2];
592 $day = (int) $dateArray[1];
593 $month = (int) $monthInt;
594 }
595 if ($dateType == 16) {
596 $dateArray = explode('-', $value);
597 if (count($dateArray) != 3) {
598 $dateArray = explode('/', $value);
599 }
600
601 if (count($dateArray) == 3) {
602 $monthInt = 0;
603 $fullMonths = self::getFullMonthNames();
604 foreach ($fullMonths as $key => $val) {
605 if (strtolower($dateArray[1]) == strtolower($val)) {
606 $monthInt = $key;
607 break;
608 }
609 }
610 if (!$monthInt) {
611 $abbrMonths = self::getAbbrMonthNames();
612 foreach ($abbrMonths as $key => $val) {
613 if (strtolower(trim($dateArray[1], ".")) == strtolower($val)) {
614 $monthInt = $key;
615 break;
616 }
617 }
618 }
619 if (!$monthInt) {
620 $monthInt = $dateArray[1];
621 }
622
623 $year = (int) $dateArray[2];
624 $day = (int) $dateArray[0];
625 $month = (int) $monthInt;
626 }
627 else {
628 return FALSE;
629 }
630 }
631 if ($dateType == 32) {
632 $formattedDate = explode("/", $value);
633 if (count($formattedDate) == 3) {
634 $year = (int) $formattedDate[2];
635 $month = (int) $formattedDate[1];
636 $day = (int) $formattedDate[0];
637 }
638 else {
639 return FALSE;
640 }
641 }
642
643 $month = ($month < 10) ? "0" . "$month" : $month;
644 $day = ($day < 10) ? "0" . "$day" : $day;
645
646 $year = (int ) $year;
647 // simple heuristic to determine what century to use
648 // 00 - 20 is always 2000 - 2020
649 // 21 - 99 is always 1921 - 1999
650 if ($year < 21) {
651 $year = (strlen($year) == 1) ? $cen . '0' . $year : $cen . $year;
652 }
653 elseif ($year < 100) {
654 $year = $prevCen . $year;
655 }
656
657 if ($params[$dateParam]) {
658 $params[$dateParam] = "$year$month$day";
659 }
660 //if month is invalid return as error
661 if ($month !== '00' && $month <= 12) {
662 return TRUE;
663 }
664 return FALSE;
665 }
666
5bc392e6
EM
667 /**
668 * @param $date
669 *
670 * @return bool
671 */
6a488035
TO
672 static function isDate(&$date) {
673 if (CRM_Utils_System::isNull($date)) {
674 return FALSE;
675 }
676 return TRUE;
677 }
678
5bc392e6
EM
679 /**
680 * @param null $timeStamp
681 *
682 * @return bool|string
683 */
6a488035
TO
684 static function currentDBDate($timeStamp = NULL) {
685 return $timeStamp ? date('YmdHis', $timeStamp) : date('YmdHis');
686 }
687
5bc392e6
EM
688 /**
689 * @param $date
690 * @param null $now
691 *
692 * @return bool
693 */
6a488035
TO
694 static function overdue($date, $now = NULL) {
695 $mysqlDate = self::isoToMysql($date);
696 if (!$now) {
697 $now = self::currentDBDate();
698 }
699 else {
700 $now = self::isoToMysql($now);
701 }
702
703 return ($mysqlDate >= $now) ? FALSE : TRUE;
704 }
705
706 /**
707 * Function to get customized today
708 *
709 * This function is used for getting customized today. To get
710 * actuall today pass 'dayParams' as null. or else pass the day,
711 * month, year values as array values
712 * Example: $dayParams = array(
713 'day' => '25', 'month' => '10',
714 * 'year' => '2007' );
715 *
716 * @param Array $dayParams Array of the day, month, year
717 * values.
718 * @param string $format expected date format( default
719 * format is 2007-12-21 )
720 *
721 * @return string Return the customized todays date (Y-m-d)
722 * @static
723 */
724 static function getToday($dayParams = NULL, $format = "Y-m-d") {
725 if (is_null($dayParams) || empty($dayParams)) {
726 $today = date($format);
727 }
728 else {
729 $today = date($format, mktime(0, 0, 0,
730 $dayParams['month'],
731 $dayParams['day'],
732 $dayParams['year']
733 ));
734 }
735
736 return $today;
737 }
738
739 /**
740 * Function to find whether today's date lies in
741 * the given range
742 *
743 * @param date $startDate start date for the range
744 * @param date $endDate end date for the range
745 *
746 * @return true todays date is in the given date range
747 * @static
748 */
749 static function getRange($startDate, $endDate) {
750 $today = date("Y-m-d");
751 $mysqlStartDate = self::isoToMysql($startDate);
752 $mysqlEndDate = self::isoToMysql($endDate);
753 $mysqlToday = self::isoToMysql($today);
754
755 if ((isset($mysqlStartDate) && isset($mysqlEndDate)) && (($mysqlToday >= $mysqlStartDate) && ($mysqlToday <= $mysqlEndDate))) {
756 return TRUE;
757 }
758 elseif ((isset($mysqlStartDate) && !isset($mysqlEndDate)) && (($mysqlToday >= $mysqlStartDate))) {
759 return TRUE;
760 }
761 elseif ((!isset($mysqlStartDate) && isset($mysqlEndDate)) && (($mysqlToday <= $mysqlEndDate))) {
762 return TRUE;
763 }
764 return FALSE;
765 }
766
767 /**
768 * Function to get start date and end from
769 * the given relative term and unit
770 *
f4aaa82a
EM
771 * @param date $relative eg: term.unit
772 *
773 * @param $from
774 * @param $to
6a488035
TO
775 *
776 * @return array start date, end date
777 * @static
778 */
779 static function getFromTo($relative, $from, $to) {
780 if ($relative) {
781 list($term, $unit) = explode('.', $relative);
782 $dateRange = self::relativeToAbsolute($term, $unit);
783 $from = $dateRange['from'];
784 //Take only Date Part, Sometime Time part is also present in 'to'
785 $to = substr($dateRange['to'], 0, 8);
786 }
787
788 $from = self::processDate($from);
789 $to = self::processDate($to, '235959');
790
791 return array($from, $to);
792 }
793
794 /**
795 * Function to calculate Age in Years if greater than one year else in months
796 *
797 * @param date $birthDate Birth Date
798 *
799 * @return int array $results contains years or months
800 * @access public
801 * @static
802 */
803 static public function calculateAge($birthDate) {
804 $results = array();
805 $formatedBirthDate = CRM_Utils_Date::customFormat($birthDate, '%Y-%m-%d');
806
807 $bDate = explode('-', $formatedBirthDate);
808 $birthYear = $bDate[0];
809 $birthMonth = $bDate[1];
810 $birthDay = $bDate[2];
811 $year_diff = date("Y") - $birthYear;
812
813 // don't calculate age CRM-3143
814 if ($birthYear == '1902') {
815 return $results;
816 }
817
818 switch ($year_diff) {
819 case 1:
820 $month = (12 - $birthMonth) + date("m");
821 if ($month < 12) {
822 if (date("d") < $birthDay) {
823 $month--;
824 }
825 $results['months'] = $month;
826 }
827 elseif ($month == 12 && (date("d") < $birthDay)) {
828 $results['months'] = $month - 1;
829 }
830 else {
831 $results['years'] = $year_diff;
832 }
833 break;
834
835 case 0:
836 $month = date("m") - $birthMonth;
837 $results['months'] = $month;
838 break;
839
840 default:
841 $results['years'] = $year_diff;
842 if ((date("m") < $birthMonth) || (date("m") == $birthMonth) && (date("d") < $birthDay)) {
843 $results['years']--;
844 }
845 }
846
847 return $results;
848 }
849
850 /**
851 * Function to calculate next payment date according to provided unit & interval
852 *
f4aaa82a 853 * @param string $unit frequency unit like year,month, week etc..
6a488035 854 *
f4aaa82a 855 * @param int $interval frequency interval.
6a488035 856 *
f4aaa82a
EM
857 * @param array $date start date of pledge.
858 *
859 * @param bool $dontCareTime
6a488035
TO
860 *
861 * @return array $result contains new date with added interval
862 * @access public
863 */
864 static function intervalAdd($unit, $interval, $date, $dontCareTime = FALSE) {
865 if (is_array($date)) {
866 $hour = CRM_Utils_Array::value('H', $date);
867 $minute = CRM_Utils_Array::value('i', $date);
868 $second = CRM_Utils_Array::value('s', $date);
869 $month = CRM_Utils_Array::value('M', $date);
870 $day = CRM_Utils_Array::value('d', $date);
871 $year = CRM_Utils_Array::value('Y', $date);
872 }
873 else {
874 extract(date_parse($date));
875 }
876 $date = mktime($hour, $minute, $second, $month, $day, $year);
877 switch ($unit) {
878 case 'year':
879 $date = mktime($hour, $minute, $second, $month, $day, $year + $interval);
880 break;
881
882 case 'month':
883 $date = mktime($hour, $minute, $second, $month + $interval, $day, $year);
884 break;
885
886 case 'week':
887 $interval = $interval * 7;
888 $date = mktime($hour, $minute, $second, $month, $day + $interval, $year);
889 break;
890
891 case 'day':
892 $date = mktime($hour, $minute, $second, $month, $day + $interval, $year);
893 break;
894
895 case 'second':
896 $date = mktime($hour, $minute, $second + $interval, $month, $day, $year);
897 break;
898 }
899
900 $scheduleDate = explode("-", date("n-j-Y-H-i-s", $date));
901
902 $date = array();
903 $date['M'] = $scheduleDate[0];
904 $date['d'] = $scheduleDate[1];
905 $date['Y'] = $scheduleDate[2];
906 if ($dontCareTime == FALSE) {
907 $date['H'] = $scheduleDate[3];
908 $date['i'] = $scheduleDate[4];
909 $date['s'] = $scheduleDate[5];
910 }
911 return $date;
912 }
913
914 /**
915 * function to check given format is valid for bith date.
916 * and retrun supportable birth date format w/ qf mapping.
917 *
918 * @param $format given format ( eg 'M Y', 'Y M' )
919 * return array of qfMapping and date parts for date format.
f4aaa82a
EM
920 *
921 * @return array|null|string
6a488035
TO
922 */
923 static function &checkBirthDateFormat($format = NULL) {
924 $birthDateFormat = NULL;
925 if (!$format) {
926 $birthDateFormat = self::getDateFormat('birth');
927 }
928
929 $supportableFormats = array(
930 'mm/dd' => '%B %E%f',
931 'dd-mm' => '%E%f %B',
932 'yy-mm' => '%Y %B',
933 'M yy' => '%b %Y',
934 'yy' => '%Y',
935 'dd/mm/yy' => '%E%f %B %Y',
936 );
937
938 if (array_key_exists($birthDateFormat, $supportableFormats)) {
939 $birthDateFormat = array('qfMapping' => $supportableFormats[$birthDateFormat]);
940 }
941
942 return $birthDateFormat;
943 }
944
945 /**
946 * resolves the given relative time interval into finite time limits
947 *
948 * @param array $relativeTerm relative time frame like this, previous, etc
949 * @param int $unit frequency unit like year, month, week etc..
950 *
951 * @return array $dateRange start date and end date for the relative time frame
952 * @static
953 */
954 static function relativeToAbsolute($relativeTerm, $unit) {
955 $now = getDate();
956 $from = $to = $dateRange = array();
957 $from['H'] = $from['i'] = $from['s'] = 0;
958
959 switch ($unit) {
960 case 'year':
961 switch ($relativeTerm) {
962 case 'this':
963 $from['d'] = $from['M'] = 1;
964 $to['d'] = 31;
965 $to['M'] = 12;
966 $to['Y'] = $from['Y'] = $now['year'];
967 break;
968
969 case 'previous':
970 $from['M'] = $from['d'] = 1;
971 $to['d'] = 31;
972 $to['M'] = 12;
973 $to['Y'] = $from['Y'] = $now['year'] - 1;
974 break;
975
976 case 'previous_before':
977 $from['M'] = $from['d'] = 1;
978 $to['d'] = 31;
979 $to['M'] = 12;
980 $to['Y'] = $from['Y'] = $now['year'] - 2;
981 break;
982
983 case 'previous_2':
984 $from['M'] = $from['d'] = 1;
985 $to['d'] = 31;
986 $to['M'] = 12;
987 $from['Y'] = $now['year'] - 2;
988 $to['Y'] = $now['year'] - 1;
989 break;
990
991 case 'earlier':
992 $to['d'] = 31;
993 $to['M'] = 12;
994 $to['Y'] = $now['year'] - 1;
995 unset($from);
996 break;
997
998 case 'greater':
999 $from['M'] = $from['d'] = 1;
1000 $from['Y'] = $now['year'];
1001 unset($to);
1002 break;
1003
52f15bd6 1004 case 'greater_previous':
1005 $from['d'] = 31;
1006 $from['M'] = 12;
1007 $from['Y'] = $now['year'] - 1;
1008 unset($to);
1009 break;
1010
6a488035
TO
1011 case 'ending':
1012 $to['d'] = $now['mday'];
1013 $to['M'] = $now['mon'];
1014 $to['Y'] = $now['year'];
1015 $to['H'] = 23;
1016 $to['i'] = $to['s'] = 59;
1017 $from = self::intervalAdd('year', -1, $to);
1018 $from = self::intervalAdd('second', 1, $from);
1019 break;
e902edd1 1020
1021 case 'current':
1022 $from['M'] = $from['d'] = 1;
1023 $from['Y'] = $now['year'];
1024 $to['H'] = 23;
1025 $to['i'] = $to['s'] = 59;
1026 $to['d'] = $now['mday'];
1027 $to['M'] = $now['mon'];
1028 $to['Y'] = $now['year'];
1029 break;
e2c3163d 1030
1031 case 'ending_2':
1032 $to['d'] = $now['mday'];
1033 $to['M'] = $now['mon'];
1034 $to['Y'] = $now['year'];
1035 $to['H'] = 23;
1036 $to['i'] = $to['s'] = 59;
1037 $from = self::intervalAdd('year', -2, $to);
1038 $from = self::intervalAdd('second', 1, $from);
1039 break;
1040
1041 case 'ending_3':
1042 $to['d'] = $now['mday'];
1043 $to['M'] = $now['mon'];
1044 $to['Y'] = $now['year'];
1045 $to['H'] = 23;
1046 $to['i'] = $to['s'] = 59;
1047 $from = self::intervalAdd('year', -3, $to);
1048 $from = self::intervalAdd('second', 1, $from);
1049 break;
c5670e9e 1050
1051 case 'less':
1052 $to['d'] = 31;
1053 $to['M'] = 12;
1054 $to['Y'] = $now['year'];
1055 unset($from);
1056 break;
1057
1058 case 'next':
1059 $from['M'] = $from['d'] = 1;
1060 $to['d'] = 31;
1061 $to['M'] = 12;
1062 $to['Y'] = $from['Y'] = $now['year'] + 1;
1063 break;
1064
1065 case 'starting':
1066 $from['d'] = $now['mday'];
1067 $from['M'] = $now['mon'];
1068 $from['Y'] = $now['year'];
1069 $to['d'] = $now['mday'] - 1;
1070 $to['M'] = $now['mon'];
1071 $to['Y'] = $now['year'] + 1;
1072 break;
6a488035
TO
1073 }
1074 break;
1075
1076 case 'fiscal_year':
1077 $config = CRM_Core_Config::singleton();
1078 $from['d'] = $config->fiscalYearStart['d'];
1079 $from['M'] = $config->fiscalYearStart['M'];
1080 $fYear = self::calculateFiscalYear($from['d'], $from['M']);
1081 switch ($relativeTerm) {
1082 case 'this':
1083 $from['Y'] = $fYear;
1084 $fiscalYear = mktime(0, 0, 0, $from['M'], $form['d'], $from['Y'] + 1);
1085 $fiscalEnd = explode('-', date("Y-m-d", $fiscalYear));
1086
1087 $to['d'] = $fiscalEnd['2'];
1088 $to['M'] = $fiscalEnd['1'];
1089 $to['Y'] = $fiscalEnd['0'];
1090 break;
1091
1092 case 'previous':
1093 $from['Y'] = $fYear - 1;
1094 $fiscalYear = mktime(0, 0, 0, $from['M'], $form['d'], $from['Y'] + 1);
1095 $fiscalEnd = explode('-', date("Y-m-d", $fiscalYear));
c5670e9e 1096 $to['d'] = $fiscalEnd['2'];
1097 $to['M'] = $fiscalEnd['1'];
1098 $to['Y'] = $fiscalEnd['0'];
1099 break;
6a488035 1100
c5670e9e 1101 case 'next':
1102 $from['Y'] = $fYear + 1;
1103 $fiscalYear = mktime(0, 0, 0, $from['M'], $from['d'], $from['Y'] + 1);
1104 $fiscalEnd = explode('-', date("Y-m-d", $fiscalYear));
6a488035
TO
1105 $to['d'] = $fiscalEnd['2'];
1106 $to['M'] = $fiscalEnd['1'];
1107 $to['Y'] = $fiscalEnd['0'];
1108 break;
1109 }
1110 break;
1111
1112 case 'quarter':
1113 switch ($relativeTerm) {
1114 case 'this':
1115
1116 $quarter = ceil($now['mon'] / 3);
1117 $from['d'] = 1;
1118 $from['M'] = (3 * $quarter) - 2;
1119 $to['M'] = 3 * $quarter;
1120 $to['Y'] = $from['Y'] = $now['year'];
1121 $to['d'] = date('t', mktime(0, 0, 0, $to['M'], 1, $now['year']));
1122 break;
1123
1124 case 'previous':
1125 $difference = 1;
1126 $quarter = ceil($now['mon'] / 3);
1127 $quarter = $quarter - $difference;
1128 $subtractYear = 0;
1129 if ($quarter <= 0) {
1130 $subtractYear = 1;
1131 $quarter += 4;
1132 }
1133 $from['d'] = 1;
1134 $from['M'] = (3 * $quarter) - 2;
1135 $to['M'] = 3 * $quarter;
1136 $to['Y'] = $from['Y'] = $now['year'] - $subtractYear;
1137 $to['d'] = date('t', mktime(0, 0, 0, $to['M'], 1, $to['Y']));
1138 break;
1139
1140 case 'previous_before':
1141 $difference = 2;
1142 $quarter = ceil($now['mon'] / 3);
1143 $quarter = $quarter - $difference;
d75f2f47 1144 $subtractYear = 0;
6a488035
TO
1145 if ($quarter <= 0) {
1146 $subtractYear = 1;
1147 $quarter += 4;
1148 }
1149 $from['d'] = 1;
1150 $from['M'] = (3 * $quarter) - 2;
1151 $to['M'] = 3 * $quarter;
1152 $to['Y'] = $from['Y'] = $now['year'] - $subtractYear;
1153 $to['d'] = date('t', mktime(0, 0, 0, $to['M'], 1, $to['Y']));
1154 break;
1155
1156 case 'previous_2':
1157 $difference = 2;
1158 $quarter = ceil($now['mon'] / 3);
1159 $current_quarter = $quarter;
1160 $quarter = $quarter - $difference;
1161 $subtractYear = 0;
1162 if ($quarter <= 0) {
1163 $subtractYear = 1;
1164 $quarter += 4;
1165 }
1166 $from['d'] = 1;
1167 $from['M'] = (3 * $quarter) - 2;
1168 switch ($current_quarter) {
1169 case 1:
1170 $to['M'] = (4 * $quarter);
1171 break;
1172
1173 case 2:
1174 $to['M'] = (4 * $quarter) + 3;
1175 break;
1176
1177 case 3:
1178 $to['M'] = (4 * $quarter) + 2;
1179 break;
1180
1181 case 4:
1182 $to['M'] = (4 * $quarter) + 1;
1183 break;
1184 }
1185 $to['Y'] = $from['Y'] = $now['year'] - $subtractYear;
1186 if ($to['M'] > 12) {
1187 $to['M'] = 3 * ($quarter - 3);
1188 $to['Y'] = $now['year'];
1189 }
1190 $to['d'] = date('t', mktime(0, 0, 0, $to['M'], 1, $to['Y']));
1191 break;
1192
1193 case 'earlier':
1194 $quarter = ceil($now['mon'] / 3) - 1;
d75f2f47 1195 $subtractYear = 0;
6a488035
TO
1196 if ($quarter <= 0) {
1197 $subtractYear = 1;
1198 $quarter += 4;
1199 }
1200 $to['M'] = 3 * $quarter;
1201 $to['Y'] = $from['Y'] = $now['year'] - $subtractYear;
1202 $to['d'] = date('t', mktime(0, 0, 0, $to['M'], 1, $to['Y']));
1203 unset($from);
1204 break;
1205
1206 case 'greater':
1207 $quarter = ceil($now['mon'] / 3);
1208 $from['d'] = 1;
1209 $from['M'] = (3 * $quarter) - 2;
1210 $from['Y'] = $now['year'];
1211 unset($to);
1212 break;
d75f2f47 1213
52f15bd6 1214 case 'greater_previous':
1215 $quarter = ceil($now['mon'] / 3) - 1;
d75f2f47 1216 $subtractYear = 0;
52f15bd6 1217 if ($quarter <= 0) {
1218 $subtractYear = 1;
1219 $quarter += 4;
1220 }
1221 $from['M'] = 3 * $quarter;
1222 $from['Y'] = $from['Y'] = $now['year'] - $subtractYear;
1223 $from['d'] = date('t', mktime(0, 0, 0, $from['M'], 1, $from['Y']));
1224 unset($to);
1225 break;
6a488035
TO
1226
1227 case 'ending':
1228 $to['d'] = $now['mday'];
1229 $to['M'] = $now['mon'];
1230 $to['Y'] = $now['year'];
1231 $to['H'] = 23;
1232 $to['i'] = $to['s'] = 59;
1233 $from = self::intervalAdd('month', -3, $to);
1234 $from = self::intervalAdd('second', 1, $from);
1235 break;
e902edd1 1236
1237 case 'current':
1238 $quarter = ceil($now['mon'] / 3);
1239 $from['d'] = 1;
1240 $from['M'] = (3 * $quarter) - 2;
1241 $from['Y'] = $now['year'];
1242 $to['d'] = $now['mday'];
1243 $to['M'] = $now['mon'];
1244 $to['Y'] = $now['year'];
1245 $to['H'] = 23;
1246 $to['i'] = $to['s'] = 59;
1247 break;
c5670e9e 1248
1249 case 'less':
1250 $quarter = ceil($now['mon'] / 3);
1251 $to['M'] = 3 * $quarter;
1252 $to['Y'] = $now['year'];
1253 $to['d'] = date('t', mktime(0, 0, 0, $to['M'], 1, $now['year']));
1254 unset($from);
1255 break;
1256
1257 case 'next':
1258 $difference = -1;
1259 $subtractYear = 0;
1260 $quarter = ceil($now['mon'] / 3);
1261 $quarter = $quarter - $difference;
1262 //CRM-14550 QA Fix
1263 if($quarter > 4) {
1264 $now['year'] = $now['year'] + 1;
1265 $quarter = 1;
1266 }
1267 if ($quarter <= 0) {
1268 $subtractYear = 1;
1269 $quarter += 4;
1270 }
1271 $from['d'] = 1;
1272 $from['M'] = (3 * $quarter) - 2;
1273 $to['M'] = 3 * $quarter;
1274 $to['Y'] = $from['Y'] = $now['year'] - $subtractYear;
1275 $to['d'] = date('t', mktime(0, 0, 0, $to['M'], 1, $to['Y']));
1276 break;
6a488035
TO
1277 }
1278 break;
1279
1280 case 'month':
1281 switch ($relativeTerm) {
1282 case 'this':
1283 $from['d'] = 1;
1284 $to['d'] = date('t', mktime(0, 0, 0, $now['mon'], 1, $now['year']));
1285 $from['M'] = $to['M'] = $now['mon'];
1286 $from['Y'] = $to['Y'] = $now['year'];
1287 break;
1288
1289 case 'previous':
1290 $from['d'] = 1;
1291 if ($now['mon'] == 1) {
1292 $from['M'] = $to['M'] = 12;
1293 $from['Y'] = $to['Y'] = $now['year'] - 1;
1294 }
1295 else {
1296 $from['M'] = $to['M'] = $now['mon'] - 1;
1297 $from['Y'] = $to['Y'] = $now['year'];
1298 }
1299 $to['d'] = date('t', mktime(0, 0, 0, $to['M'], 1, $to['Y']));
1300 break;
1301
1302 case 'previous_before':
1303 $from['d'] = 1;
1304 if ($now['mon'] < 3) {
1305 $from['M'] = $to['M'] = 10 + $now['mon'];
1306 $from['Y'] = $to['Y'] = $now['year'] - 1;
1307 }
1308 else {
1309 $from['M'] = $to['M'] = $now['mon'] - 2;
1310 $from['Y'] = $to['Y'] = $now['year'];
1311 }
1312 $to['d'] = date('t', mktime(0, 0, 0, $to['M'], 1, $to['Y']));
1313 break;
1314
1315 case 'previous_2':
1316 $from['d'] = 1;
1317 if ($now['mon'] < 3) {
1318 $from['M'] = 10 + $now['mon'];
1319 $from['Y'] = $now['year'] - 1;
1320 }
1321 else {
1322 $from['M'] = $now['mon'] - 2;
1323 $from['Y'] = $now['year'];
1324 }
1325
1326 if ($now['mon'] == 1) {
1327 $to['M'] = 12;
1328 $to['Y'] = $now['year'] - 1;
1329 }
1330 else {
1331 $to['M'] = $now['mon'] - 1;
1332 $to['Y'] = $now['year'];
1333 }
1334
1335 $to['d'] = date('t', mktime(0, 0, 0, $to['M'], 1, $to['Y']));
1336 break;
1337
1338 case 'earlier':
1339 //before end of past month
1340 if ($now['mon'] == 1) {
1341 $to['M'] = 12;
1342 $to['Y'] = $now['year'] - 1;
1343 }
1344 else {
1345 $to['M'] = $now['mon'] - 1;
1346 $to['Y'] = $now['year'];
1347 }
1348
1349 $to['d'] = date('t', mktime(0, 0, 0, $to['M'], 1, $to['Y']));
1350 unset($from);
1351 break;
1352
1353 case 'greater':
1354 $from['d'] = 1;
1355 $from['M'] = $now['mon'];;
1356 $from['Y'] = $now['year'];
1357 unset($to);
1358 break;
1359
52f15bd6 1360 case 'greater_previous':
1361 //from end of past month
1362 if ($now['mon'] == 1) {
1363 $from['M'] = 12;
1364 $from['Y'] = $now['year'] - 1;
1365 }
1366 else {
1367 $from['M'] = $now['mon'] - 1;
1368 $from['Y'] = $now['year'];
1369 }
1370
1371 $from['d'] = date('t', mktime(0, 0, 0, $from['M'], 1, $from['Y']));
1372 unset($to);
1373 break;
1374
6a488035
TO
1375 case 'ending':
1376 $to['d'] = $now['mday'];
1377 $to['M'] = $now['mon'];
1378 $to['Y'] = $now['year'];
1379 $to['H'] = 23;
1380 $to['i'] = $to['s'] = 59;
1381 $from = self::intervalAdd('month', -1, $to);
1382 $from = self::intervalAdd('second', 1, $from);
1383 break;
e902edd1 1384
1385 case 'current':
1386 $from['d'] = 1;
1387 $from['M'] = $now['mon'];;
1388 $from['Y'] = $now['year'];
1389 $to['d'] = $now['mday'];
1390 $to['M'] = $now['mon'];
1391 $to['Y'] = $now['year'];
1392 $to['H'] = 23;
1393 $to['i'] = $to['s'] = 59;
1394 break;
c5670e9e 1395
1396 case 'less':
1397 //CRM-14550 QA Fix
1398 $to['Y'] = $now['year'];
1399 $to['M'] = $now['mon'];
1400 $to['d'] = date('t', mktime(0, 0, 0, $now['mon'], 1, $now['year']));
1401 unset($from);
1402 break;
1403
1404 case 'next':
1405 $from['d'] = 1;
1406 if ($now['mon'] == 12) {
1407 $from['M'] = $to['M'] = 1;
1408 $from['Y'] = $to['Y'] = $now['year'] + 1;
1409 }
1410 else {
1411 $from['M'] = $to['M'] = $now['mon'] + 1;
1412 $from['Y'] = $to['Y'] = $now['year'];
1413 }
1414 $to['d'] = date('t', mktime(0, 0, 0, $to['M'], 1, $to['Y']));
1415 break;
1416
1417 case 'starting':
1418 $from['d'] = $now['mday'];
1419 $from['M'] = $now['mon'];
1420 $from['Y'] = $now['year'];
1421 $from['H'] = 00;
1422 $from['i'] = $to['s'] = 00;
1423 $to = self::intervalAdd('month', +1, $from);
1424 $to = self::intervalAdd('second',-1, $to);
1425 break;
6a488035
TO
1426 }
1427 break;
1428
1429 case 'week':
1430 switch ($relativeTerm) {
1431 case 'this':
1432 $from['d'] = $now['mday'];
1433 $from['M'] = $now['mon'];
1434 $from['Y'] = $now['year'];
1435 $from = self::intervalAdd('day', -1 * ($now['wday']), $from);
1436 $to = self::intervalAdd('day', 6, $from);
1437 break;
1438
1439 case 'previous':
1440 $from['d'] = $now['mday'];
1441 $from['M'] = $now['mon'];
1442 $from['Y'] = $now['year'];
1443 $from = self::intervalAdd('day', -1 * ($now['wday']) - 7, $from);
1444 $to = self::intervalAdd('day', 6, $from);
1445 break;
1446
1447 case 'previous_before':
1448 $from['d'] = $now['mday'];
1449 $from['M'] = $now['mon'];
1450 $from['Y'] = $now['year'];
1451 $from = self::intervalAdd('day', -1 * ($now['wday']) - 14, $from);
1452 $to = self::intervalAdd('day', 6, $from);
1453 break;
1454
1455 case 'previous_2':
1456 $from['d'] = $now['mday'];
1457 $from['M'] = $now['mon'];
1458 $from['Y'] = $now['year'];
1459 $from = self::intervalAdd('day', -1 * ($now['wday']) - 14, $from);
1460 $to = self::intervalAdd('day', 13, $from);
1461 break;
1462
1463 case 'earlier':
1464 $to['d'] = $now['mday'];
1465 $to['M'] = $now['mon'];
1466 $to['Y'] = $now['year'];
1467 $to = self::intervalAdd('day', -1 * ($now['wday']) - 1, $to);
1468 unset($from);
1469 break;
1470
1471 case 'greater':
1472 $from['d'] = $now['mday'];
1473 $from['M'] = $now['mon'];
1474 $from['Y'] = $now['year'];
1475 $from = self::intervalAdd('day', -1 * ($now['wday']), $from);
1476 unset($to);
1477 break;
1478
52f15bd6 1479 case 'greater_previous':
1480 $from['d'] = $now['mday'];
1481 $from['M'] = $now['mon'];
1482 $from['Y'] = $now['year'];
1483 $from = self::intervalAdd('day', -1 * ($now['wday']) - 1, $from);
1484 unset($to);
1485 break;
1486
6a488035
TO
1487 case 'ending':
1488 $to['d'] = $now['mday'];
1489 $to['M'] = $now['mon'];
1490 $to['Y'] = $now['year'];
1491 $to['H'] = 23;
1492 $to['i'] = $to['s'] = 59;
1493 $from = self::intervalAdd('day', -7, $to);
1494 $from = self::intervalAdd('second', 1, $from);
1495 break;
e902edd1 1496
1497 case 'current':
1498 $from['d'] = $now['mday'];
1499 $from['M'] = $now['mon'];
1500 $from['Y'] = $now['year'];
1501 $from = self::intervalAdd('day', -1 * ($now['wday']), $from);
1502 $to['d'] = $now['mday'];
1503 $to['M'] = $now['mon'];
1504 $to['Y'] = $now['year'];
1505 $to['H'] = 23;
1506 $to['i'] = $to['s'] = 59;
1507 break;
c5670e9e 1508
1509 case 'less':
1510 $to['d'] = $now['mday'];
1511 $to['M'] = $now['mon'];
1512 $to['Y'] = $now['year'];
1513 //CRM-14550 QA Fix
1514 $to = self::intervalAdd('day', -1 * ($now['wday']) + 6, $to);
1515 unset($from);
1516 break;
1517
1518 case 'next':
1519 $from['d'] = $now['mday'];
1520 $from['M'] = $now['mon'];
1521 $from['Y'] = $now['year'];
1522 $from = self::intervalAdd('day', -1 * ($now['wday']) + 7, $from);
1523 $to = self::intervalAdd('day', +6, $from);
1524 break;
1525
1526 case 'starting':
1527 $from['d'] = $now['mday'];
1528 $from['M'] = $now['mon'];
1529 $from['Y'] = $now['year'];
1530 $from['H'] = 00;
1531 $from['i'] = $to['s'] = 00;
1532 $to = self::intervalAdd('day', +7, $from);
1533 $to = self::intervalAdd('second', -1, $to);
1534 break;
6a488035
TO
1535 }
1536 break;
1537
1538 case 'day':
1539 switch ($relativeTerm) {
1540 case 'this':
1541 $from['d'] = $to['d'] = $now['mday'];
1542 $from['M'] = $to['M'] = $now['mon'];
1543 $from['Y'] = $to['Y'] = $now['year'];
1544 break;
1545
1546 case 'previous':
1547 $from['d'] = $now['mday'];
1548 $from['M'] = $now['mon'];
1549 $from['Y'] = $now['year'];
1550 $from = self::intervalAdd('day', -1, $from);
1551 $to['d'] = $from['d'];
1552 $to['M'] = $from['M'];
1553 $to['Y'] = $from['Y'];
1554 break;
1555
1556 case 'previous_before':
1557 $from['d'] = $now['mday'];
1558 $from['M'] = $now['mon'];
1559 $from['Y'] = $now['year'];
1560 $from = self::intervalAdd('day', -2, $from);
1561 $to['d'] = $from['d'];
1562 $to['M'] = $from['M'];
1563 $to['Y'] = $from['Y'];
1564 break;
1565
1566 case 'previous_2':
1567 $from['d'] = $to['d'] = $now['mday'];
1568 $from['M'] = $to['M'] = $now['mon'];
1569 $from['Y'] = $to['Y'] = $now['year'];
1570 $from = self::intervalAdd('day', -2, $from);
1571 $to = self::intervalAdd('day', -1, $to);
1572 break;
1573
1574 case 'earlier':
1575 $to['d'] = $now['mday'];
1576 $to['M'] = $now['mon'];
1577 $to['Y'] = $now['year'];
1578 unset($from);
1579 break;
1580
1581 case 'greater':
1582 $from['d'] = $now['mday'];
1583 $from['M'] = $now['mon'];;
1584 $from['Y'] = $now['year'];
1585 unset($to);
1586 break;
c5670e9e 1587
1588 case 'starting':
1589 $to['d'] = $now['mday'];
1590 $to['M'] = $now['mon'];
1591 $to['Y'] = $now['year'];
1592 $to = self::intervalAdd('day', +1, $to);
1593 $from['d'] = $to['d'];
1594 $from['M'] = $to['M'];
1595 $from['Y'] = $to['Y'];
1596 break;
1597
6a488035
TO
1598 }
1599 break;
1600 }
1601
1602 foreach (array(
1603 'from', 'to') as $item) {
1604 if (!empty($$item)) {
1605 $dateRange[$item] = self::format($$item);
1606 }
1607 else {
1608 $dateRange[$item] = NULL;
1609 }
1610 }
1611 return $dateRange;
1612 }
1613
1614 /**
1615 * Function to calculate current fiscal year based on the fiscal month and day
1616 *
1617 * @param int $fyDate Fiscal start date
1618 *
1619 * @param int $fyMonth Fiscal Start Month
1620 *
1621 * @return int $fy Current Fiscl Year
1622 * @access public
1623 * @static
1624 */
1625 static function calculateFiscalYear($fyDate, $fyMonth) {
1626 $date = date("Y-m-d");
1627 $currentYear = date("Y");
1628
1629 //recalculate the date because month 4::04 make the difference
1630 $fiscalYear = explode('-', date("Y-m-d", mktime(0, 0, 0, $fyMonth, $fyDate, $currentYear)));
1631 $fyDate = $fiscalYear[2];
1632 $fyMonth = $fiscalYear[1];
1633 $fyStartDate = date("Y-m-d", mktime(0, 0, 0, $fyMonth, $fyDate, $currentYear));
1634
1635 if ($fyStartDate > $date) {
1636 $fy = intval(intval($currentYear) - 1);
1637 }
1638 else {
1639 $fy = intval($currentYear);
1640 }
1641 return $fy;
1642 }
1643
1644 /**
1645 * Function to process date, convert to mysql format
1646 *
f4aaa82a
EM
1647 * @param string $date date string
1648 * @param string $time time string
1649 * @param bool|string $returnNullString 'null' needs to be returned
6a488035 1650 * so that db oject will set null in db
f4aaa82a 1651 * @param string $format expected return date format.( default is mysql )
6a488035 1652 *
f4aaa82a 1653 * @return string $mysqlDate date format that is excepted by mysql
6a488035
TO
1654 */
1655 static function processDate($date, $time = NULL, $returnNullString = FALSE, $format = 'YmdHis') {
1656 $mysqlDate = NULL;
1657
1658 if ($returnNullString) {
1659 $mysqlDate = 'null';
1660 }
1661
1662 if (trim($date)) {
1663 $mysqlDate = date($format, strtotime($date . ' ' . $time));
1664 }
1665
1666 return $mysqlDate;
1667 }
1668
1669 /**
1670 * Function to convert mysql to date plugin format
1671 *
f4aaa82a
EM
1672 * @param string $mysqlDate date string
1673 *
1674 * @param null $formatType
1675 * @param null $format
1676 * @param null $timeFormat
6a488035 1677 *
f4aaa82a 1678 * @return array $date and time
6a488035
TO
1679 */
1680 static function setDateDefaults($mysqlDate = NULL, $formatType = NULL, $format = NULL, $timeFormat = NULL) {
1681 // if date is not passed assume it as today
1682 if (!$mysqlDate) {
1683 $mysqlDate = date('Y-m-d G:i:s');
1684 }
1685
1686 $config = CRM_Core_Config::singleton();
1687 if ($formatType) {
1688 // get actual format
1689 $params = array('name' => $formatType);
1690 $values = array();
1691 CRM_Core_DAO::commonRetrieve('CRM_Core_DAO_PreferencesDate', $params, $values);
1692
1693 if ($values['date_format']) {
1694 $format = $values['date_format'];
1695 }
1696
1697 if (isset($values['time_format'])) {
1698 $timeFormat = $values['time_format'];
1699 }
1700 }
1701
1702 // now we set display date using js, hence we should always setdefault
1703 // 'm/d/Y' format. So that submitted value is alwats mm/dd/YY format
1704 // note that for date display we dynamically create text field
1705 /*
1706 if ( !$format ) {
1707 $format = $config->dateInputFormat;
1708 }
1709
1710 // get actual format
1711 $actualPHPFormats = CRM_Core_SelectValues::datePluginToPHPFormats( );
1712 $dateFormat = CRM_Utils_Array::value( $format, $actualPHPFormats );
1713 */
1714
1715
1716 $dateFormat = 'm/d/Y';
1717 $date = date($dateFormat, strtotime($mysqlDate));
1718
1719 if (!$timeFormat) {
1720 $timeFormat = $config->timeInputFormat;
1721 }
1722
1723 $actualTimeFormat = "g:iA";
1724 $appendZeroLength = 7;
1725 if ($timeFormat > 1) {
1726 $actualTimeFormat = "G:i";
1727 $appendZeroLength = 5;
1728 }
1729
1730 $time = date($actualTimeFormat, strtotime($mysqlDate));
1731
1732 // need to append zero for hours < 10
1733 if (strlen($time) < $appendZeroLength) {
1734 $time = '0' . $time;
1735 }
1736
1737 return array($date, $time);
1738 }
1739
1740 /**
1741 * Function get date format
1742 *
1743 * @param string $formatType Date name e.g. birth
1744 *
1745 * @return string $format
1746 */
1747 static function getDateFormat($formatType = NULL) {
1748 $format = NULL;
1749 if ($formatType) {
395d8dc6 1750 $format = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_PreferencesDate',
6a488035
TO
1751 $formatType, 'date_format', 'name'
1752 );
1753 }
1754
1755 if (!$format) {
1756 $config = CRM_Core_Config::singleton();
1757 $format = $config->dateInputFormat;
1758 }
1759 return $format;
1760 }
1761
cf142e47
DL
1762 /**
1763 * Get the time in UTC for the current time. You can optionally send an offset from the current time if needed
1764 *
1765 * @param $offset int the offset from the current time in seconds
1766 *
1767 * @return the time in UTC
1768 * @static
1769 * @public
1770 */
1771 static function getUTCTime($offset = 0) {
6a488035
TO
1772 $originalTimezone = date_default_timezone_get();
1773 date_default_timezone_set('UTC');
1774 $time = time() + $offset;
1775 $now = date('YmdHis', $time);
1776 date_default_timezone_set($originalTimezone);
1777 return $now;
1778 }
1779
1780
5bc392e6
EM
1781 /**
1782 * @param $date
1783 * @param $dateType
1784 *
1785 * @return null|string
1786 */
6a488035
TO
1787 static function formatDate($date, $dateType) {
1788 $formattedDate = NULL;
1789 if (empty($date)) {
1790 return $formattedDate;
1791 }
1792
1793 //1. first convert date to default format.
1794 //2. append time to default formatted date (might be removed during format)
1795 //3. validate date / date time.
1796 //4. If date and time then convert to default date time format.
1797
1798 $dateKey = 'date';
1799 $dateParams = array($dateKey => $date);
1800
1801 if (CRM_Utils_Date::convertToDefaultDate($dateParams, $dateType, $dateKey)) {
1802 $dateVal = $dateParams[$dateKey];
1803 $ruleName = 'date';
1804 if ($dateType == 1) {
1805 $matches = array();
1806 if (preg_match("/(\s(([01]\d)|[2][0-3]):([0-5]\d))$/", $date, $matches)) {
1807 $ruleName = 'dateTime';
1808 if (strpos($date, '-') !== FALSE) {
1809 $dateVal .= array_shift($matches);
1810 }
1811 }
1812 }
1813
1814 // validate date.
0e6e8724 1815 $valid = CRM_Utils_Rule::$ruleName($dateVal);
6a488035
TO
1816
1817 if ($valid) {
1818 //format date and time to default.
1819 if ($ruleName == 'dateTime') {
1820 $dateVal = CRM_Utils_Date::customFormat(preg_replace("/(:|\s)?/", "", $dateVal), '%Y%m%d%H%i');
1821 //hack to add seconds
1822 $dateVal .= '00';
1823 }
1824 $formattedDate = $dateVal;
1825 }
1826 }
1827
1828 return $formattedDate;
1829 }
1830
1831}
1832