594bbc0aec52dc0a4787da32046cb1d0f3b8b967
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2014
39 class CRM_Utils_Date
{
42 * format a date by padding it with leading '0'.
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
48 * @return string - formatted string for date
52 static function format($date, $separator = '', $invalidDate = 0) {
53 if (is_numeric($date) &&
54 ((strlen($date) == 8) ||
(strlen($date) == 14))
59 if (!is_array($date) ||
60 CRM_Utils_System
::isNull($date) ||
66 $date['Y'] = (int ) $date['Y'];
67 if ($date['Y'] < 1000 ||
$date['Y'] > 2999) {
71 if (array_key_exists('m', $date)) {
72 $date['M'] = $date['m'];
74 elseif (array_key_exists('F', $date)) {
75 $date['M'] = $date['F'];
78 if (!empty($date['M'])) {
79 $date['M'] = (int ) $date['M'];
80 if ($date['M'] < 1 ||
$date['M'] > 12) {
88 if (!empty($date['d'])) {
89 $date['d'] = (int ) $date['d'];
95 if (!checkdate($date['M'], $date['d'], $date['Y'])) {
99 $date['M'] = sprintf('%02d', $date['M']);
100 $date['d'] = sprintf('%02d', $date['d']);
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
108 // we have time too..
109 if (!empty($date['h'])) {
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;
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
121 $date['h'] = (int ) $date['h'];
127 // in 24-hour format the hour is under the 'H' key
128 if (!empty($date['H'])) {
129 $date['H'] = (int) $date['H'];
135 if (!empty($date['i'])) {
136 $date['i'] = (int ) $date['i'];
142 if ($date['h'] == 0 && $date['H'] != 0) {
143 $date['h'] = $date['H'];
146 if (!empty($date['s'])) {
147 $date['s'] = (int ) $date['s'];
153 $date['h'] = sprintf('%02d', $date['h']);
154 $date['i'] = sprintf('%02d', $date['i']);
155 $date['s'] = sprintf('%02d', $date['s']);
160 $time .= $date['h'] . $separator . $date['i'] . $separator . $date['s'];
163 return $date['Y'] . $separator . $date['M'] . $separator . $date['d'] . $time;
167 * return abbreviated weekday names according to the locale
169 * @return array 0-based array with abbreviated weekday names
173 static function &getAbbrWeekdayNames() {
174 static $abbrWeekdayNames;
175 if (!isset($abbrWeekdayNames)) {
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));
184 return $abbrWeekdayNames;
188 * return full weekday names according to the locale
190 * @return array 0-based array with full weekday names
194 static function &getFullWeekdayNames() {
195 static $fullWeekdayNames;
196 if (!isset($fullWeekdayNames)) {
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));
205 return $fullWeekdayNames;
209 * return abbreviated month names according to the locale
213 * @return array 1-based array with abbreviated month names
217 static function &getAbbrMonthNames($month = FALSE) {
218 static $abbrMonthNames;
219 if (!isset($abbrMonthNames)) {
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));
228 return $abbrMonthNames[$month];
230 return $abbrMonthNames;
234 * return full month names according to the locale
236 * @return array 1-based array with full month names
240 static function &getFullMonthNames() {
241 static $fullMonthNames;
242 if (!isset($fullMonthNames)) {
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));
250 return $fullMonthNames;
258 static function unixTime($string) {
259 if (empty($string)) {
262 $parsedDate = date_parse($string);
263 return mktime(CRM_Utils_Array
::value('hour', $parsedDate),
264 CRM_Utils_Array
::value('minute', $parsedDate),
266 CRM_Utils_Array
::value('month', $parsedDate),
267 CRM_Utils_Array
::value('day', $parsedDate),
268 CRM_Utils_Array
::value('year', $parsedDate)
273 * create a date and time string in a provided format
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')
291 * @param string $dateString
292 * @param string $format the output format
293 * @param array $dateParts an array with the desired date parts
295 * @internal param string $date date and time in 'YYYY-MM-DD hh:mm:ss' format
296 * @return string the $format-formatted $date
300 static function customFormat($dateString, $format = NULL, $dateParts = NULL) {
301 // 1-based (January) month names arrays
302 $abbrMonths = self
::getAbbrMonthNames();
303 $fullMonths = self
::getFullMonthNames();
306 $config = CRM_Core_Config
::singleton();
309 if (array_intersect(array('h', 'H'), $dateParts)) {
310 $format = $config->dateformatDatetime
;
312 elseif (array_intersect(array('d', 'j'), $dateParts)) {
313 $format = $config->dateformatFull
;
315 elseif (array_intersect(array('m', 'M'), $dateParts)) {
316 $format = $config->dateformatPartial
;
319 $format = $config->dateformatYear
;
323 if (strpos($dateString, '-')) {
324 $month = (int) substr($dateString, 5, 2);
325 $day = (int) substr($dateString, 8, 2);
328 $month = (int) substr($dateString, 4, 2);
329 $day = (int) substr($dateString, 6, 2);
332 if (strlen($dateString) > 10) {
333 $format = $config->dateformatDatetime
;
336 $format = $config->dateformatFull
;
338 elseif ($month > 0) {
339 $format = $config->dateformatPartial
;
342 $format = $config->dateformatYear
;
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);
353 $hour24 = (int) substr($dateString, 11, 2);
354 $minute = (int) substr($dateString, 14, 2);
357 $year = (int) substr($dateString, 0, 4);
358 $month = (int) substr($dateString, 4, 2);
359 $day = (int) substr($dateString, 6, 2);
361 $hour24 = (int) substr($dateString, 8, 2);
362 $minute = (int) substr($dateString, 10, 2);
365 if ($day %
10 == 1 and $day != 11) {
368 elseif ($day %
10 == 2 and $day != 12) {
371 elseif ($day %
10 == 3 and $day != 13) {
392 $hour12 = $hour24 - 12;
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,
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),
418 return strtr($format, $date);
426 * converts the date/datetime from MySQL format to ISO format
428 * @param string $mysql date/datetime in MySQL format
430 * @return string date/datetime in ISO format
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);
465 * converts the date/datetime from ISO format to MySQL format
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
470 * @param string $iso date/datetime in ISO format
472 * @return string date/datetime in MySQL format
475 static function isoToMysql($iso) {
476 $dropArray = array('-' => '', ':' => '', ' ' => '');
477 return strtr($iso, $dropArray);
481 * converts the any given date to default date format.
483 * @param array $params has given date-format
484 * @param int $dateType type of date
485 * @param string $dateParam index of params
490 static function convertToDefaultDate(&$params, $dateType, $dateParam) {
492 $cen = substr($now['year'], 0, 2);
496 if (!empty($params[$dateParam])) {
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]);
503 if (!preg_match('/^\d\d\d\d-?(\d|\d\d)-?(\d|\d\d)$/', $value)) {
509 if (!preg_match('/^(\d|\d\d)[-\/](\d|\d\d)[-\/]\d\d$/', $value)) {
515 if (!preg_match('/^(\d|\d\d)[-\/](\d|\d\d)[-\/]\d\d\d\d$/', $value)) {
521 if (!preg_match('/^[A-Za-z]*.[ \t]?\d\d\,[ \t]?\d\d\d\d$/', $value)) {
527 if (!preg_match('/^\d\d-[A-Za-z]{3}.*-\d\d$/', $value) && !preg_match('/^\d\d[-\/]\d\d[-\/]\d\d$/', $value)) {
533 if (!preg_match('/^(\d|\d\d)[-\/](\d|\d\d)[-\/]\d\d\d\d/', $value)) {
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];
546 elseif (count($formattedDate) == 1 && (strlen($value) == 8)) {
555 if ($dateType == 2 ||
$dateType == 4) {
556 $formattedDate = explode("/", $value);
557 if (count($formattedDate) != 3) {
558 $formattedDate = explode("-", $value);
560 if (count($formattedDate) == 3) {
561 $year = (int) $formattedDate[2];
562 $month = (int) $formattedDate[0];
563 $day = (int) $formattedDate[1];
569 if ($dateType == 8) {
570 $dateArray = explode(' ', $value);
572 $dateArray[1] = (int) substr($dateArray[1], 0, 2);
575 $fullMonths = self
::getFullMonthNames();
576 foreach ($fullMonths as $key => $val) {
577 if (strtolower($dateArray[0]) == strtolower($val)) {
583 $abbrMonths = self
::getAbbrMonthNames();
584 foreach ($abbrMonths as $key => $val) {
585 if (strtolower(trim($dateArray[0], ".")) == strtolower($val)) {
591 $year = (int) $dateArray[2];
592 $day = (int) $dateArray[1];
593 $month = (int) $monthInt;
595 if ($dateType == 16) {
596 $dateArray = explode('-', $value);
597 if (count($dateArray) != 3) {
598 $dateArray = explode('/', $value);
601 if (count($dateArray) == 3) {
603 $fullMonths = self
::getFullMonthNames();
604 foreach ($fullMonths as $key => $val) {
605 if (strtolower($dateArray[1]) == strtolower($val)) {
611 $abbrMonths = self
::getAbbrMonthNames();
612 foreach ($abbrMonths as $key => $val) {
613 if (strtolower(trim($dateArray[1], ".")) == strtolower($val)) {
620 $monthInt = $dateArray[1];
623 $year = (int) $dateArray[2];
624 $day = (int) $dateArray[0];
625 $month = (int) $monthInt;
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];
643 $month = ($month < 10) ?
"0" . "$month" : $month;
644 $day = ($day < 10) ?
"0" . "$day" : $day;
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
651 $year = (strlen($year) == 1) ?
$cen . '0' . $year : $cen . $year;
653 elseif ($year < 100) {
654 $year = $prevCen . $year;
657 if ($params[$dateParam]) {
658 $params[$dateParam] = "$year$month$day";
660 //if month is invalid return as error
661 if ($month !== '00' && $month <= 12) {
672 static function isDate(&$date) {
673 if (CRM_Utils_System
::isNull($date)) {
680 * @param null $timeStamp
682 * @return bool|string
684 static function currentDBDate($timeStamp = NULL) {
685 return $timeStamp ?
date('YmdHis', $timeStamp) : date('YmdHis');
694 static function overdue($date, $now = NULL) {
695 $mysqlDate = self
::isoToMysql($date);
697 $now = self
::currentDBDate();
700 $now = self
::isoToMysql($now);
703 return ($mysqlDate >= $now) ?
FALSE : TRUE;
707 * Function to get customized today
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' );
716 * @param Array $dayParams Array of the day, month, year
718 * @param string $format expected date format( default
719 * format is 2007-12-21 )
721 * @return string Return the customized todays date (Y-m-d)
724 static function getToday($dayParams = NULL, $format = "Y-m-d") {
725 if (is_null($dayParams) ||
empty($dayParams)) {
726 $today = date($format);
729 $today = date($format, mktime(0, 0, 0,
740 * Function to find whether today's date lies in
743 * @param date $startDate start date for the range
744 * @param date $endDate end date for the range
746 * @return true todays date is in the given date range
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);
755 if ((isset($mysqlStartDate) && isset($mysqlEndDate)) && (($mysqlToday >= $mysqlStartDate) && ($mysqlToday <= $mysqlEndDate))) {
758 elseif ((isset($mysqlStartDate) && !isset($mysqlEndDate)) && (($mysqlToday >= $mysqlStartDate))) {
761 elseif ((!isset($mysqlStartDate) && isset($mysqlEndDate)) && (($mysqlToday <= $mysqlEndDate))) {
768 * Function to get start date and end from
769 * the given relative term and unit
771 * @param date $relative eg: term.unit
776 * @return array start date, end date
779 static function getFromTo($relative, $from, $to) {
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);
788 $from = self
::processDate($from);
789 $to = self
::processDate($to, '235959');
791 return array($from, $to);
795 * Function to calculate Age in Years if greater than one year else in months
797 * @param date $birthDate Birth Date
799 * @return int array $results contains years or months
803 static public function calculateAge($birthDate) {
805 $formatedBirthDate = CRM_Utils_Date
::customFormat($birthDate, '%Y-%m-%d');
807 $bDate = explode('-', $formatedBirthDate);
808 $birthYear = $bDate[0];
809 $birthMonth = $bDate[1];
810 $birthDay = $bDate[2];
811 $year_diff = date("Y") - $birthYear;
813 // don't calculate age CRM-3143
814 if ($birthYear == '1902') {
818 switch ($year_diff) {
820 $month = (12 - $birthMonth) +
date("m");
822 if (date("d") < $birthDay) {
825 $results['months'] = $month;
827 elseif ($month == 12 && (date("d") < $birthDay)) {
828 $results['months'] = $month - 1;
831 $results['years'] = $year_diff;
836 $month = date("m") - $birthMonth;
837 $results['months'] = $month;
841 $results['years'] = $year_diff;
842 if ((date("m") < $birthMonth) ||
(date("m") == $birthMonth) && (date("d") < $birthDay)) {
851 * Function to calculate next payment date according to provided unit & interval
853 * @param string $unit frequency unit like year,month, week etc..
855 * @param int $interval frequency interval.
857 * @param array $date start date of pledge.
859 * @param bool $dontCareTime
861 * @return array $result contains new date with added interval
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);
874 extract(date_parse($date));
876 $date = mktime($hour, $minute, $second, $month, $day, $year);
879 $date = mktime($hour, $minute, $second, $month, $day, $year +
$interval);
883 $date = mktime($hour, $minute, $second, $month +
$interval, $day, $year);
887 $interval = $interval * 7;
888 $date = mktime($hour, $minute, $second, $month, $day +
$interval, $year);
892 $date = mktime($hour, $minute, $second, $month, $day +
$interval, $year);
896 $date = mktime($hour, $minute, $second +
$interval, $month, $day, $year);
900 $scheduleDate = explode("-", date("n-j-Y-H-i-s", $date));
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];
915 * function to check given format is valid for bith date.
916 * and retrun supportable birth date format w/ qf mapping.
918 * @param $format given format ( eg 'M Y', 'Y M' )
919 * return array of qfMapping and date parts for date format.
921 * @return array|null|string
923 static function &checkBirthDateFormat($format = NULL) {
924 $birthDateFormat = NULL;
926 $birthDateFormat = self
::getDateFormat('birth');
929 $supportableFormats = array(
930 'mm/dd' => '%B %E%f',
931 'dd-mm' => '%E%f %B',
935 'dd/mm/yy' => '%E%f %B %Y',
938 if (array_key_exists($birthDateFormat, $supportableFormats)) {
939 $birthDateFormat = array('qfMapping' => $supportableFormats[$birthDateFormat]);
942 return $birthDateFormat;
946 * resolves the given relative time interval into finite time limits
948 * @param array $relativeTerm relative time frame like this, previous, etc
949 * @param int $unit frequency unit like year, month, week etc..
951 * @return array $dateRange start date and end date for the relative time frame
954 static function relativeToAbsolute($relativeTerm, $unit) {
956 $from = $to = $dateRange = array();
957 $from['H'] = $from['i'] = $from['s'] = 0;
961 switch ($relativeTerm) {
963 $from['d'] = $from['M'] = 1;
966 $to['Y'] = $from['Y'] = $now['year'];
970 $from['M'] = $from['d'] = 1;
973 $to['Y'] = $from['Y'] = $now['year'] - 1;
976 case 'previous_before':
977 $from['M'] = $from['d'] = 1;
980 $to['Y'] = $from['Y'] = $now['year'] - 2;
984 $from['M'] = $from['d'] = 1;
987 $from['Y'] = $now['year'] - 2;
988 $to['Y'] = $now['year'] - 1;
994 $to['Y'] = $now['year'] - 1;
999 $from['M'] = $from['d'] = 1;
1000 $from['Y'] = $now['year'];
1005 $to['d'] = $now['mday'];
1006 $to['M'] = $now['mon'];
1007 $to['Y'] = $now['year'];
1009 $to['i'] = $to['s'] = 59;
1010 $from = self
::intervalAdd('year', -1, $to);
1011 $from = self
::intervalAdd('second', 1, $from);
1015 $from['M'] = $from['d'] = 1;
1016 $from['Y'] = $now['year'];
1018 $to['i'] = $to['s'] = 59;
1019 $to['d'] = $now['mday'];
1020 $to['M'] = $now['mon'];
1021 $to['Y'] = $now['year'];
1025 $to['d'] = $now['mday'];
1026 $to['M'] = $now['mon'];
1027 $to['Y'] = $now['year'];
1029 $to['i'] = $to['s'] = 59;
1030 $from = self
::intervalAdd('year', -2, $to);
1031 $from = self
::intervalAdd('second', 1, $from);
1035 $to['d'] = $now['mday'];
1036 $to['M'] = $now['mon'];
1037 $to['Y'] = $now['year'];
1039 $to['i'] = $to['s'] = 59;
1040 $from = self
::intervalAdd('year', -3, $to);
1041 $from = self
::intervalAdd('second', 1, $from);
1047 $config = CRM_Core_Config
::singleton();
1048 $from['d'] = $config->fiscalYearStart
['d'];
1049 $from['M'] = $config->fiscalYearStart
['M'];
1050 $fYear = self
::calculateFiscalYear($from['d'], $from['M']);
1051 switch ($relativeTerm) {
1053 $from['Y'] = $fYear;
1054 $fiscalYear = mktime(0, 0, 0, $from['M'], $form['d'], $from['Y'] +
1);
1055 $fiscalEnd = explode('-', date("Y-m-d", $fiscalYear));
1057 $to['d'] = $fiscalEnd['2'];
1058 $to['M'] = $fiscalEnd['1'];
1059 $to['Y'] = $fiscalEnd['0'];
1063 $from['Y'] = $fYear - 1;
1064 $fiscalYear = mktime(0, 0, 0, $from['M'], $form['d'], $from['Y'] +
1);
1065 $fiscalEnd = explode('-', date("Y-m-d", $fiscalYear));
1067 $to['d'] = $fiscalEnd['2'];
1068 $to['M'] = $fiscalEnd['1'];
1069 $to['Y'] = $fiscalEnd['0'];
1075 switch ($relativeTerm) {
1078 $quarter = ceil($now['mon'] / 3);
1080 $from['M'] = (3 * $quarter) - 2;
1081 $to['M'] = 3 * $quarter;
1082 $to['Y'] = $from['Y'] = $now['year'];
1083 $to['d'] = date('t', mktime(0, 0, 0, $to['M'], 1, $now['year']));
1088 $quarter = ceil($now['mon'] / 3);
1089 $quarter = $quarter - $difference;
1091 if ($quarter <= 0) {
1096 $from['M'] = (3 * $quarter) - 2;
1097 $to['M'] = 3 * $quarter;
1098 $to['Y'] = $from['Y'] = $now['year'] - $subtractYear;
1099 $to['d'] = date('t', mktime(0, 0, 0, $to['M'], 1, $to['Y']));
1102 case 'previous_before':
1104 $quarter = ceil($now['mon'] / 3);
1105 $quarter = $quarter - $difference;
1107 if ($quarter <= 0) {
1112 $from['M'] = (3 * $quarter) - 2;
1113 $to['M'] = 3 * $quarter;
1114 $to['Y'] = $from['Y'] = $now['year'] - $subtractYear;
1115 $to['d'] = date('t', mktime(0, 0, 0, $to['M'], 1, $to['Y']));
1120 $quarter = ceil($now['mon'] / 3);
1121 $current_quarter = $quarter;
1122 $quarter = $quarter - $difference;
1124 if ($quarter <= 0) {
1129 $from['M'] = (3 * $quarter) - 2;
1130 switch ($current_quarter) {
1132 $to['M'] = (4 * $quarter);
1136 $to['M'] = (4 * $quarter) +
3;
1140 $to['M'] = (4 * $quarter) +
2;
1144 $to['M'] = (4 * $quarter) +
1;
1147 $to['Y'] = $from['Y'] = $now['year'] - $subtractYear;
1148 if ($to['M'] > 12) {
1149 $to['M'] = 3 * ($quarter - 3);
1150 $to['Y'] = $now['year'];
1152 $to['d'] = date('t', mktime(0, 0, 0, $to['M'], 1, $to['Y']));
1156 $quarter = ceil($now['mon'] / 3) - 1;
1158 if ($quarter <= 0) {
1162 $to['M'] = 3 * $quarter;
1163 $to['Y'] = $from['Y'] = $now['year'] - $subtractYear;
1164 $to['d'] = date('t', mktime(0, 0, 0, $to['M'], 1, $to['Y']));
1169 $quarter = ceil($now['mon'] / 3);
1171 $from['M'] = (3 * $quarter) - 2;
1172 $from['Y'] = $now['year'];
1177 $to['d'] = $now['mday'];
1178 $to['M'] = $now['mon'];
1179 $to['Y'] = $now['year'];
1181 $to['i'] = $to['s'] = 59;
1182 $from = self
::intervalAdd('month', -3, $to);
1183 $from = self
::intervalAdd('second', 1, $from);
1187 $quarter = ceil($now['mon'] / 3);
1189 $from['M'] = (3 * $quarter) - 2;
1190 $from['Y'] = $now['year'];
1191 $to['d'] = $now['mday'];
1192 $to['M'] = $now['mon'];
1193 $to['Y'] = $now['year'];
1195 $to['i'] = $to['s'] = 59;
1201 switch ($relativeTerm) {
1204 $to['d'] = date('t', mktime(0, 0, 0, $now['mon'], 1, $now['year']));
1205 $from['M'] = $to['M'] = $now['mon'];
1206 $from['Y'] = $to['Y'] = $now['year'];
1211 if ($now['mon'] == 1) {
1212 $from['M'] = $to['M'] = 12;
1213 $from['Y'] = $to['Y'] = $now['year'] - 1;
1216 $from['M'] = $to['M'] = $now['mon'] - 1;
1217 $from['Y'] = $to['Y'] = $now['year'];
1219 $to['d'] = date('t', mktime(0, 0, 0, $to['M'], 1, $to['Y']));
1222 case 'previous_before':
1224 if ($now['mon'] < 3) {
1225 $from['M'] = $to['M'] = 10 +
$now['mon'];
1226 $from['Y'] = $to['Y'] = $now['year'] - 1;
1229 $from['M'] = $to['M'] = $now['mon'] - 2;
1230 $from['Y'] = $to['Y'] = $now['year'];
1232 $to['d'] = date('t', mktime(0, 0, 0, $to['M'], 1, $to['Y']));
1237 if ($now['mon'] < 3) {
1238 $from['M'] = 10 +
$now['mon'];
1239 $from['Y'] = $now['year'] - 1;
1242 $from['M'] = $now['mon'] - 2;
1243 $from['Y'] = $now['year'];
1246 if ($now['mon'] == 1) {
1248 $to['Y'] = $now['year'] - 1;
1251 $to['M'] = $now['mon'] - 1;
1252 $to['Y'] = $now['year'];
1255 $to['d'] = date('t', mktime(0, 0, 0, $to['M'], 1, $to['Y']));
1259 //before end of past month
1260 if ($now['mon'] == 1) {
1262 $to['Y'] = $now['year'] - 1;
1265 $to['M'] = $now['mon'] - 1;
1266 $to['Y'] = $now['year'];
1269 $to['d'] = date('t', mktime(0, 0, 0, $to['M'], 1, $to['Y']));
1275 $from['M'] = $now['mon'];;
1276 $from['Y'] = $now['year'];
1281 $to['d'] = $now['mday'];
1282 $to['M'] = $now['mon'];
1283 $to['Y'] = $now['year'];
1285 $to['i'] = $to['s'] = 59;
1286 $from = self
::intervalAdd('month', -1, $to);
1287 $from = self
::intervalAdd('second', 1, $from);
1292 $from['M'] = $now['mon'];;
1293 $from['Y'] = $now['year'];
1294 $to['d'] = $now['mday'];
1295 $to['M'] = $now['mon'];
1296 $to['Y'] = $now['year'];
1298 $to['i'] = $to['s'] = 59;
1304 switch ($relativeTerm) {
1306 $from['d'] = $now['mday'];
1307 $from['M'] = $now['mon'];
1308 $from['Y'] = $now['year'];
1309 $from = self
::intervalAdd('day', -1 * ($now['wday']), $from);
1310 $to = self
::intervalAdd('day', 6, $from);
1314 $from['d'] = $now['mday'];
1315 $from['M'] = $now['mon'];
1316 $from['Y'] = $now['year'];
1317 $from = self
::intervalAdd('day', -1 * ($now['wday']) - 7, $from);
1318 $to = self
::intervalAdd('day', 6, $from);
1321 case 'previous_before':
1322 $from['d'] = $now['mday'];
1323 $from['M'] = $now['mon'];
1324 $from['Y'] = $now['year'];
1325 $from = self
::intervalAdd('day', -1 * ($now['wday']) - 14, $from);
1326 $to = self
::intervalAdd('day', 6, $from);
1330 $from['d'] = $now['mday'];
1331 $from['M'] = $now['mon'];
1332 $from['Y'] = $now['year'];
1333 $from = self
::intervalAdd('day', -1 * ($now['wday']) - 14, $from);
1334 $to = self
::intervalAdd('day', 13, $from);
1338 $to['d'] = $now['mday'];
1339 $to['M'] = $now['mon'];
1340 $to['Y'] = $now['year'];
1341 $to = self
::intervalAdd('day', -1 * ($now['wday']) - 1, $to);
1346 $from['d'] = $now['mday'];
1347 $from['M'] = $now['mon'];
1348 $from['Y'] = $now['year'];
1349 $from = self
::intervalAdd('day', -1 * ($now['wday']), $from);
1354 $to['d'] = $now['mday'];
1355 $to['M'] = $now['mon'];
1356 $to['Y'] = $now['year'];
1358 $to['i'] = $to['s'] = 59;
1359 $from = self
::intervalAdd('day', -7, $to);
1360 $from = self
::intervalAdd('second', 1, $from);
1364 $from['d'] = $now['mday'];
1365 $from['M'] = $now['mon'];
1366 $from['Y'] = $now['year'];
1367 $from = self
::intervalAdd('day', -1 * ($now['wday']), $from);
1368 $to['d'] = $now['mday'];
1369 $to['M'] = $now['mon'];
1370 $to['Y'] = $now['year'];
1372 $to['i'] = $to['s'] = 59;
1378 switch ($relativeTerm) {
1380 $from['d'] = $to['d'] = $now['mday'];
1381 $from['M'] = $to['M'] = $now['mon'];
1382 $from['Y'] = $to['Y'] = $now['year'];
1386 $from['d'] = $now['mday'];
1387 $from['M'] = $now['mon'];
1388 $from['Y'] = $now['year'];
1389 $from = self
::intervalAdd('day', -1, $from);
1390 $to['d'] = $from['d'];
1391 $to['M'] = $from['M'];
1392 $to['Y'] = $from['Y'];
1395 case 'previous_before':
1396 $from['d'] = $now['mday'];
1397 $from['M'] = $now['mon'];
1398 $from['Y'] = $now['year'];
1399 $from = self
::intervalAdd('day', -2, $from);
1400 $to['d'] = $from['d'];
1401 $to['M'] = $from['M'];
1402 $to['Y'] = $from['Y'];
1406 $from['d'] = $to['d'] = $now['mday'];
1407 $from['M'] = $to['M'] = $now['mon'];
1408 $from['Y'] = $to['Y'] = $now['year'];
1409 $from = self
::intervalAdd('day', -2, $from);
1410 $to = self
::intervalAdd('day', -1, $to);
1414 $to['d'] = $now['mday'];
1415 $to['M'] = $now['mon'];
1416 $to['Y'] = $now['year'];
1421 $from['d'] = $now['mday'];
1422 $from['M'] = $now['mon'];;
1423 $from['Y'] = $now['year'];
1431 'from', 'to') as $item) {
1432 if (!empty($
$item)) {
1433 $dateRange[$item] = self
::format($
$item);
1436 $dateRange[$item] = NULL;
1443 * Function to calculate current fiscal year based on the fiscal month and day
1445 * @param int $fyDate Fiscal start date
1447 * @param int $fyMonth Fiscal Start Month
1449 * @return int $fy Current Fiscl Year
1453 static function calculateFiscalYear($fyDate, $fyMonth) {
1454 $date = date("Y-m-d");
1455 $currentYear = date("Y");
1457 //recalculate the date because month 4::04 make the difference
1458 $fiscalYear = explode('-', date("Y-m-d", mktime(0, 0, 0, $fyMonth, $fyDate, $currentYear)));
1459 $fyDate = $fiscalYear[2];
1460 $fyMonth = $fiscalYear[1];
1461 $fyStartDate = date("Y-m-d", mktime(0, 0, 0, $fyMonth, $fyDate, $currentYear));
1463 if ($fyStartDate > $date) {
1464 $fy = intval(intval($currentYear) - 1);
1467 $fy = intval($currentYear);
1473 * Function to process date, convert to mysql format
1475 * @param string $date date string
1476 * @param string $time time string
1477 * @param bool|string $returnNullString 'null' needs to be returned
1478 * so that db oject will set null in db
1479 * @param string $format expected return date format.( default is mysql )
1481 * @return string $mysqlDate date format that is excepted by mysql
1483 static function processDate($date, $time = NULL, $returnNullString = FALSE, $format = 'YmdHis') {
1486 if ($returnNullString) {
1487 $mysqlDate = 'null';
1491 $mysqlDate = date($format, strtotime($date . ' ' . $time));
1498 * Function to convert mysql to date plugin format
1500 * @param string $mysqlDate date string
1502 * @param null $formatType
1503 * @param null $format
1504 * @param null $timeFormat
1506 * @return array $date and time
1508 static function setDateDefaults($mysqlDate = NULL, $formatType = NULL, $format = NULL, $timeFormat = NULL) {
1509 // if date is not passed assume it as today
1511 $mysqlDate = date('Y-m-d G:i:s');
1514 $config = CRM_Core_Config
::singleton();
1516 // get actual format
1517 $params = array('name' => $formatType);
1519 CRM_Core_DAO
::commonRetrieve('CRM_Core_DAO_PreferencesDate', $params, $values);
1521 if ($values['date_format']) {
1522 $format = $values['date_format'];
1525 if (isset($values['time_format'])) {
1526 $timeFormat = $values['time_format'];
1530 // now we set display date using js, hence we should always setdefault
1531 // 'm/d/Y' format. So that submitted value is alwats mm/dd/YY format
1532 // note that for date display we dynamically create text field
1535 $format = $config->dateInputFormat;
1538 // get actual format
1539 $actualPHPFormats = CRM_Core_SelectValues::datePluginToPHPFormats( );
1540 $dateFormat = CRM_Utils_Array::value( $format, $actualPHPFormats );
1544 $dateFormat = 'm/d/Y';
1545 $date = date($dateFormat, strtotime($mysqlDate));
1548 $timeFormat = $config->timeInputFormat
;
1551 $actualTimeFormat = "g:iA";
1552 $appendZeroLength = 7;
1553 if ($timeFormat > 1) {
1554 $actualTimeFormat = "G:i";
1555 $appendZeroLength = 5;
1558 $time = date($actualTimeFormat, strtotime($mysqlDate));
1560 // need to append zero for hours < 10
1561 if (strlen($time) < $appendZeroLength) {
1562 $time = '0' . $time;
1565 return array($date, $time);
1569 * Function get date format
1571 * @param string $formatType Date name e.g. birth
1573 * @return string $format
1575 static function getDateFormat($formatType = NULL) {
1578 $format = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_PreferencesDate',
1579 $formatType, 'date_format', 'name'
1584 $config = CRM_Core_Config
::singleton();
1585 $format = $config->dateInputFormat
;
1591 * Get the time in UTC for the current time. You can optionally send an offset from the current time if needed
1593 * @param $offset int the offset from the current time in seconds
1595 * @return the time in UTC
1599 static function getUTCTime($offset = 0) {
1600 $originalTimezone = date_default_timezone_get();
1601 date_default_timezone_set('UTC');
1602 $time = time() +
$offset;
1603 $now = date('YmdHis', $time);
1604 date_default_timezone_set($originalTimezone);
1613 * @return null|string
1615 static function formatDate($date, $dateType) {
1616 $formattedDate = NULL;
1618 return $formattedDate;
1621 //1. first convert date to default format.
1622 //2. append time to default formatted date (might be removed during format)
1623 //3. validate date / date time.
1624 //4. If date and time then convert to default date time format.
1627 $dateParams = array($dateKey => $date);
1629 if (CRM_Utils_Date
::convertToDefaultDate($dateParams, $dateType, $dateKey)) {
1630 $dateVal = $dateParams[$dateKey];
1632 if ($dateType == 1) {
1634 if (preg_match("/(\s(([01]\d)|[2][0-3]):([0-5]\d))$/", $date, $matches)) {
1635 $ruleName = 'dateTime';
1636 if (strpos($date, '-') !== FALSE) {
1637 $dateVal .= array_shift($matches);
1643 $valid = CRM_Utils_Rule
::$ruleName($dateVal);
1646 //format date and time to default.
1647 if ($ruleName == 'dateTime') {
1648 $dateVal = CRM_Utils_Date
::customFormat(preg_replace("/(:|\s)?/", "", $dateVal), '%Y%m%d%H%i');
1649 //hack to add seconds
1652 $formattedDate = $dateVal;
1656 return $formattedDate;