3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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-2013
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 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 (CRM_Utils_Array
::value('M', $date)) {
79 $date['M'] = (int ) $date['M'];
80 if ($date['M'] < 1 ||
$date['M'] > 12) {
88 if (CRM_Utils_Array
::value('d', $date)) {
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 (CRM_Utils_Array
::value('h', $date)) {
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 (CRM_Utils_Array
::value('H', $date)) {
129 $date['H'] = (int) $date['H'];
135 if (CRM_Utils_Array
::value('i', $date)) {
136 $date['i'] = (int ) $date['i'];
142 if ($date['h'] == 0 && $date['H'] != 0) {
143 $date['h'] = $date['H'];
146 if (CRM_Utils_Array
::value('s', $date)) {
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
211 * @return array 1-based array with abbreviated month names
215 static function &getAbbrMonthNames($month = FALSE) {
216 static $abbrMonthNames;
217 if (!isset($abbrMonthNames)) {
219 // set LC_TIME and build the arrays from locale-provided names
220 CRM_Core_I18n
::setLcTime();
221 for ($i = 1; $i <= 12; $i++
) {
222 $abbrMonthNames[$i] = strftime('%b', mktime(0, 0, 0, $i, 10, 1970));
226 return $abbrMonthNames[$month];
228 return $abbrMonthNames;
232 * return full month names according to the locale
234 * @return array 1-based array with full month names
238 static function &getFullMonthNames() {
239 static $fullMonthNames;
240 if (!isset($fullMonthNames)) {
242 // set LC_TIME and build the arrays from locale-provided names
243 CRM_Core_I18n
::setLcTime();
244 for ($i = 1; $i <= 12; $i++
) {
245 $fullMonthNames[$i] = strftime('%B', mktime(0, 0, 0, $i, 10, 1970));
248 return $fullMonthNames;
251 static function unixTime($string) {
252 if (empty($string)) {
255 $parsedDate = date_parse($string);
256 return mktime(CRM_Utils_Array
::value('hour', $parsedDate),
257 CRM_Utils_Array
::value('minute', $parsedDate),
259 CRM_Utils_Array
::value('month', $parsedDate),
260 CRM_Utils_Array
::value('day', $parsedDate),
261 CRM_Utils_Array
::value('year', $parsedDate)
266 * create a date and time string in a provided format
268 * %b - abbreviated month name ('Jan'..'Dec')
269 * %B - full month name ('January'..'December')
270 * %d - day of the month as a decimal number, 0-padded ('01'..'31')
271 * %e - day of the month as a decimal number, blank-padded (' 1'..'31')
272 * %E - day of the month as a decimal number ('1'..'31')
273 * %f - English ordinal suffix for the day of the month ('st', 'nd', 'rd', 'th')
274 * %H - hour in 24-hour format, 0-padded ('00'..'23')
275 * %I - hour in 12-hour format, 0-padded ('01'..'12')
276 * %k - hour in 24-hour format, blank-padded (' 0'..'23')
277 * %l - hour in 12-hour format, blank-padded (' 1'..'12')
278 * %m - month as a decimal number, 0-padded ('01'..'12')
279 * %M - minute, 0-padded ('00'..'60')
280 * %p - lowercase ante/post meridiem ('am', 'pm')
281 * %P - uppercase ante/post meridiem ('AM', 'PM')
282 * %Y - year as a decimal number including the century ('2005')
284 * @param string $date date and time in 'YYYY-MM-DD hh:mm:ss' format
285 * @param string $format the output format
286 * @param array $dateParts an array with the desired date parts
288 * @return string the $format-formatted $date
292 static function customFormat($dateString, $format = NULL, $dateParts = NULL) {
293 // 1-based (January) month names arrays
294 $abbrMonths = self
::getAbbrMonthNames();
295 $fullMonths = self
::getFullMonthNames();
298 $config = CRM_Core_Config
::singleton();
301 if (array_intersect(array('h', 'H'), $dateParts)) {
302 $format = $config->dateformatDatetime
;
304 elseif (array_intersect(array('d', 'j'), $dateParts)) {
305 $format = $config->dateformatFull
;
307 elseif (array_intersect(array('m', 'M'), $dateParts)) {
308 $format = $config->dateformatPartial
;
311 $format = $config->dateformatYear
;
315 if (strpos($dateString, '-')) {
316 $month = (int) substr($dateString, 5, 2);
317 $day = (int) substr($dateString, 8, 2);
320 $month = (int) substr($dateString, 4, 2);
321 $day = (int) substr($dateString, 6, 2);
324 if (strlen($dateString) > 10) {
325 $format = $config->dateformatDatetime
;
328 $format = $config->dateformatFull
;
330 elseif ($month > 0) {
331 $format = $config->dateformatPartial
;
334 $format = $config->dateformatYear
;
340 if (strpos($dateString, '-')) {
341 $year = (int) substr($dateString, 0, 4);
342 $month = (int) substr($dateString, 5, 2);
343 $day = (int) substr($dateString, 8, 2);
345 $hour24 = (int) substr($dateString, 11, 2);
346 $minute = (int) substr($dateString, 14, 2);
349 $year = (int) substr($dateString, 0, 4);
350 $month = (int) substr($dateString, 4, 2);
351 $day = (int) substr($dateString, 6, 2);
353 $hour24 = (int) substr($dateString, 8, 2);
354 $minute = (int) substr($dateString, 10, 2);
357 if ($day %
10 == 1 and $day != 11) {
360 elseif ($day %
10 == 2 and $day != 12) {
363 elseif ($day %
10 == 3 and $day != 13) {
384 $hour12 = $hour24 - 12;
390 '%b' => CRM_Utils_Array
::value($month, $abbrMonths),
391 '%B' => CRM_Utils_Array
::value($month, $fullMonths),
392 '%d' => $day > 9 ?
$day : '0' . $day,
393 '%e' => $day > 9 ?
$day : ' ' . $day,
396 '%H' => $hour24 > 9 ?
$hour24 : '0' . $hour24,
397 '%h' => $hour12 > 9 ?
$hour12 : '0' . $hour12,
398 '%I' => $hour12 > 9 ?
$hour12 : '0' . $hour12,
399 '%k' => $hour24 > 9 ?
$hour24 : ' ' . $hour24,
400 '%l' => $hour12 > 9 ?
$hour12 : ' ' . $hour12,
401 '%m' => $month > 9 ?
$month : '0' . $month,
402 '%M' => $minute > 9 ?
$minute : '0' . $minute,
403 '%i' => $minute > 9 ?
$minute : '0' . $minute,
404 '%p' => strtolower($type),
410 return strtr($format, $date);
418 * converts the date/datetime from MySQL format to ISO format
420 * @param string $mysql date/datetime in MySQL format
422 * @return string date/datetime in ISO format
425 static function mysqlToIso($mysql) {
426 $year = substr($mysql, 0, 4);
427 $month = substr($mysql, 4, 2);
428 $day = substr($mysql, 6, 2);
429 $hour = substr($mysql, 8, 2);
430 $minute = substr($mysql, 10, 2);
431 $second = substr($mysql, 12, 2);
457 * converts the date/datetime from ISO format to MySQL format
459 * @param string $iso date/datetime in ISO format
461 * @return string date/datetime in MySQL format
464 static function isoToMysql($iso) {
465 $dropArray = array('-' => '', ':' => '', ' ' => '');
466 return strtr($iso, $dropArray);
470 * converts the any given date to default date format.
472 * @param array $params has given date-format
473 * @param int $dateType type of date
474 * @param string $dateParam index of params
477 static function convertToDefaultDate(&$params, $dateType, $dateParam) {
479 $cen = substr($now['year'], 0, 2);
483 if (CRM_Utils_Array
::value($dateParam, $params)) {
484 // suppress hh:mm or hh:mm:ss if it exists CRM-7957
485 $value = preg_replace("/(\s(([01]\d)|[2][0-3])(:([0-5]\d)){1,2})$/", "", $params[$dateParam]);
490 if (!preg_match('/^\d\d\d\d-?(\d|\d\d)-?(\d|\d\d)$/', $value)) {
496 if (!preg_match('/^(\d|\d\d)[-\/](\d|\d\d)[-\/]\d\d$/', $value)) {
502 if (!preg_match('/^(\d|\d\d)[-\/](\d|\d\d)[-\/]\d\d\d\d$/', $value)) {
508 if (!preg_match('/^[A-Za-z]*.[ \t]?\d\d\,[ \t]?\d\d\d\d$/', $value)) {
514 if (!preg_match('/^\d\d-[A-Za-z]{3}.*-\d\d$/', $value) && !preg_match('/^\d\d[-\/]\d\d[-\/]\d\d$/', $value)) {
520 if (!preg_match('/^(\d|\d\d)[-\/](\d|\d\d)[-\/]\d\d\d\d/', $value)) {
526 if ($dateType == 1) {
527 $formattedDate = explode("-", $value);
528 if (count($formattedDate) == 3) {
529 $year = (int) $formattedDate[0];
530 $month = (int) $formattedDate[1];
531 $day = (int) $formattedDate[2];
533 elseif (count($formattedDate) == 1 && (strlen($value) == 8)) {
542 if ($dateType == 2 ||
$dateType == 4) {
543 $formattedDate = explode("/", $value);
544 if (count($formattedDate) != 3) {
545 $formattedDate = explode("-", $value);
547 if (count($formattedDate) == 3) {
548 $year = (int) $formattedDate[2];
549 $month = (int) $formattedDate[0];
550 $day = (int) $formattedDate[1];
556 if ($dateType == 8) {
557 $dateArray = explode(' ', $value);
559 $dateArray[1] = (int) substr($dateArray[1], 0, 2);
562 $fullMonths = self
::getFullMonthNames();
563 foreach ($fullMonths as $key => $val) {
564 if (strtolower($dateArray[0]) == strtolower($val)) {
570 $abbrMonths = self
::getAbbrMonthNames();
571 foreach ($abbrMonths as $key => $val) {
572 if (strtolower(trim($dateArray[0], ".")) == strtolower($val)) {
578 $year = (int) $dateArray[2];
579 $day = (int) $dateArray[1];
580 $month = (int) $monthInt;
582 if ($dateType == 16) {
583 $dateArray = explode('-', $value);
584 if (count($dateArray) != 3) {
585 $dateArray = explode('/', $value);
588 if (count($dateArray) == 3) {
590 $fullMonths = self
::getFullMonthNames();
591 foreach ($fullMonths as $key => $val) {
592 if (strtolower($dateArray[1]) == strtolower($val)) {
598 $abbrMonths = self
::getAbbrMonthNames();
599 foreach ($abbrMonths as $key => $val) {
600 if (strtolower(trim($dateArray[1], ".")) == strtolower($val)) {
607 $monthInt = $dateArray[1];
610 $year = (int) $dateArray[2];
611 $day = (int) $dateArray[0];
612 $month = (int) $monthInt;
618 if ($dateType == 32) {
619 $formattedDate = explode("/", $value);
620 if (count($formattedDate) == 3) {
621 $year = (int) $formattedDate[2];
622 $month = (int) $formattedDate[1];
623 $day = (int) $formattedDate[0];
630 $month = ($month < 10) ?
"0" . "$month" : $month;
631 $day = ($day < 10) ?
"0" . "$day" : $day;
633 $year = (int ) $year;
634 // simple heuristic to determine what century to use
635 // 00 - 20 is always 2000 - 2020
636 // 21 - 99 is always 1921 - 1999
638 $year = (strlen($year) == 1) ?
$cen . '0' . $year : $cen . $year;
640 elseif ($year < 100) {
641 $year = $prevCen . $year;
644 if ($params[$dateParam]) {
645 $params[$dateParam] = "$year$month$day";
647 //if month is invalid return as error
648 if ($month !== '00' && $month <= 12) {
654 static function isDate(&$date) {
655 if (CRM_Utils_System
::isNull($date)) {
661 static function currentDBDate($timeStamp = NULL) {
662 return $timeStamp ?
date('YmdHis', $timeStamp) : date('YmdHis');
665 static function overdue($date, $now = NULL) {
666 $mysqlDate = self
::isoToMysql($date);
668 $now = self
::currentDBDate();
671 $now = self
::isoToMysql($now);
674 return ($mysqlDate >= $now) ?
FALSE : TRUE;
678 * Function to get customized today
680 * This function is used for getting customized today. To get
681 * actuall today pass 'dayParams' as null. or else pass the day,
682 * month, year values as array values
683 * Example: $dayParams = array(
684 'day' => '25', 'month' => '10',
685 * 'year' => '2007' );
687 * @param Array $dayParams Array of the day, month, year
689 * @param string $format expected date format( default
690 * format is 2007-12-21 )
692 * @return string Return the customized todays date (Y-m-d)
695 static function getToday($dayParams = NULL, $format = "Y-m-d") {
696 if (is_null($dayParams) ||
empty($dayParams)) {
697 $today = date($format);
700 $today = date($format, mktime(0, 0, 0,
711 * Function to find whether today's date lies in
714 * @param date $startDate start date for the range
715 * @param date $endDate end date for the range
717 * @return true todays date is in the given date range
720 static function getRange($startDate, $endDate) {
721 $today = date("Y-m-d");
722 $mysqlStartDate = self
::isoToMysql($startDate);
723 $mysqlEndDate = self
::isoToMysql($endDate);
724 $mysqlToday = self
::isoToMysql($today);
726 if ((isset($mysqlStartDate) && isset($mysqlEndDate)) && (($mysqlToday >= $mysqlStartDate) && ($mysqlToday <= $mysqlEndDate))) {
729 elseif ((isset($mysqlStartDate) && !isset($mysqlEndDate)) && (($mysqlToday >= $mysqlStartDate))) {
732 elseif ((!isset($mysqlStartDate) && isset($mysqlEndDate)) && (($mysqlToday <= $mysqlEndDate))) {
739 * Function to get start date and end from
740 * the given relative term and unit
742 * @param date $relative eg: term.unit
744 * @return array start date, end date
747 static function getFromTo($relative, $from, $to) {
749 list($term, $unit) = explode('.', $relative);
750 $dateRange = self
::relativeToAbsolute($term, $unit);
751 $from = $dateRange['from'];
752 //Take only Date Part, Sometime Time part is also present in 'to'
753 $to = substr($dateRange['to'], 0, 8);
756 $from = self
::processDate($from);
757 $to = self
::processDate($to, '235959');
759 return array($from, $to);
763 * Function to calculate Age in Years if greater than one year else in months
765 * @param date $birthDate Birth Date
767 * @return int array $results contains years or months
771 static public function calculateAge($birthDate) {
773 $formatedBirthDate = CRM_Utils_Date
::customFormat($birthDate, '%Y-%m-%d');
775 $bDate = explode('-', $formatedBirthDate);
776 $birthYear = $bDate[0];
777 $birthMonth = $bDate[1];
778 $birthDay = $bDate[2];
779 $year_diff = date("Y") - $birthYear;
781 // don't calculate age CRM-3143
782 if ($birthYear == '1902') {
786 switch ($year_diff) {
788 $month = (12 - $birthMonth) +
date("m");
790 if (date("d") < $birthDay) {
793 $results['months'] = $month;
795 elseif ($month == 12 && (date("d") < $birthDay)) {
796 $results['months'] = $month - 1;
799 $results['years'] = $year_diff;
804 $month = date("m") - $birthMonth;
805 $results['months'] = $month;
809 $results['years'] = $year_diff;
810 if ((date("m") < $birthMonth) ||
(date("m") == $birthMonth) && (date("d") < $birthDay)) {
819 * Function to calculate next payment date according to provided unit & interval
821 * @param string $unit frequency unit like year,month, week etc..
823 * @param int $interval frequency interval.
825 * @param array $date start date of pledge.
827 * @return array $result contains new date with added interval
830 static function intervalAdd($unit, $interval, $date, $dontCareTime = FALSE) {
831 if (is_array($date)) {
832 $hour = CRM_Utils_Array
::value('H', $date);
833 $minute = CRM_Utils_Array
::value('i', $date);
834 $second = CRM_Utils_Array
::value('s', $date);
835 $month = CRM_Utils_Array
::value('M', $date);
836 $day = CRM_Utils_Array
::value('d', $date);
837 $year = CRM_Utils_Array
::value('Y', $date);
840 extract(date_parse($date));
842 $date = mktime($hour, $minute, $second, $month, $day, $year);
845 $date = mktime($hour, $minute, $second, $month, $day, $year +
$interval);
849 $date = mktime($hour, $minute, $second, $month +
$interval, $day, $year);
853 $interval = $interval * 7;
854 $date = mktime($hour, $minute, $second, $month, $day +
$interval, $year);
858 $date = mktime($hour, $minute, $second, $month, $day +
$interval, $year);
862 $date = mktime($hour, $minute, $second +
$interval, $month, $day, $year);
866 $scheduleDate = explode("-", date("n-j-Y-H-i-s", $date));
869 $date['M'] = $scheduleDate[0];
870 $date['d'] = $scheduleDate[1];
871 $date['Y'] = $scheduleDate[2];
872 if ($dontCareTime == FALSE) {
873 $date['H'] = $scheduleDate[3];
874 $date['i'] = $scheduleDate[4];
875 $date['s'] = $scheduleDate[5];
881 * function to check given format is valid for bith date.
882 * and retrun supportable birth date format w/ qf mapping.
884 * @param $format given format ( eg 'M Y', 'Y M' )
885 * return array of qfMapping and date parts for date format.
887 static function &checkBirthDateFormat($format = NULL) {
888 $birthDateFormat = NULL;
890 $birthDateFormat = self
::getDateFormat('birth');
893 $supportableFormats = array(
894 'mm/dd' => '%B %E%f',
895 'dd-mm' => '%E%f %B',
899 'dd/mm/yy' => '%E%f %B %Y',
902 if (array_key_exists($birthDateFormat, $supportableFormats)) {
903 $birthDateFormat = array('qfMapping' => $supportableFormats[$birthDateFormat]);
906 return $birthDateFormat;
910 * resolves the given relative time interval into finite time limits
912 * @param array $relativeTerm relative time frame like this, previous, etc
913 * @param int $unit frequency unit like year, month, week etc..
915 * @return array $dateRange start date and end date for the relative time frame
918 static function relativeToAbsolute($relativeTerm, $unit) {
920 $from = $to = $dateRange = array();
921 $from['H'] = $from['i'] = $from['s'] = 0;
925 switch ($relativeTerm) {
927 $from['d'] = $from['M'] = 1;
930 $to['Y'] = $from['Y'] = $now['year'];
934 $from['M'] = $from['d'] = 1;
937 $to['Y'] = $from['Y'] = $now['year'] - 1;
940 case 'previous_before':
941 $from['M'] = $from['d'] = 1;
944 $to['Y'] = $from['Y'] = $now['year'] - 2;
948 $from['M'] = $from['d'] = 1;
951 $from['Y'] = $now['year'] - 2;
952 $to['Y'] = $now['year'] - 1;
958 $to['Y'] = $now['year'] - 1;
963 $from['M'] = $from['d'] = 1;
964 $from['Y'] = $now['year'];
969 $to['d'] = $now['mday'];
970 $to['M'] = $now['mon'];
971 $to['Y'] = $now['year'];
973 $to['i'] = $to['s'] = 59;
974 $from = self
::intervalAdd('year', -1, $to);
975 $from = self
::intervalAdd('second', 1, $from);
979 $from['M'] = $from['d'] = 1;
980 $from['Y'] = $now['year'];
982 $to['i'] = $to['s'] = 59;
983 $to['d'] = $now['mday'];
984 $to['M'] = $now['mon'];
985 $to['Y'] = $now['year'];
991 $config = CRM_Core_Config
::singleton();
992 $from['d'] = $config->fiscalYearStart
['d'];
993 $from['M'] = $config->fiscalYearStart
['M'];
994 $fYear = self
::calculateFiscalYear($from['d'], $from['M']);
995 switch ($relativeTerm) {
998 $fiscalYear = mktime(0, 0, 0, $from['M'], $form['d'], $from['Y'] +
1);
999 $fiscalEnd = explode('-', date("Y-m-d", $fiscalYear));
1001 $to['d'] = $fiscalEnd['2'];
1002 $to['M'] = $fiscalEnd['1'];
1003 $to['Y'] = $fiscalEnd['0'];
1007 $from['Y'] = $fYear - 1;
1008 $fiscalYear = mktime(0, 0, 0, $from['M'], $form['d'], $from['Y'] +
1);
1009 $fiscalEnd = explode('-', date("Y-m-d", $fiscalYear));
1011 $to['d'] = $fiscalEnd['2'];
1012 $to['M'] = $fiscalEnd['1'];
1013 $to['Y'] = $fiscalEnd['0'];
1019 switch ($relativeTerm) {
1022 $quarter = ceil($now['mon'] / 3);
1024 $from['M'] = (3 * $quarter) - 2;
1025 $to['M'] = 3 * $quarter;
1026 $to['Y'] = $from['Y'] = $now['year'];
1027 $to['d'] = date('t', mktime(0, 0, 0, $to['M'], 1, $now['year']));
1032 $quarter = ceil($now['mon'] / 3);
1033 $quarter = $quarter - $difference;
1035 if ($quarter <= 0) {
1040 $from['M'] = (3 * $quarter) - 2;
1041 $to['M'] = 3 * $quarter;
1042 $to['Y'] = $from['Y'] = $now['year'] - $subtractYear;
1043 $to['d'] = date('t', mktime(0, 0, 0, $to['M'], 1, $to['Y']));
1046 case 'previous_before':
1048 $quarter = ceil($now['mon'] / 3);
1049 $quarter = $quarter - $difference;
1050 if ($quarter <= 0) {
1055 $from['M'] = (3 * $quarter) - 2;
1056 $to['M'] = 3 * $quarter;
1057 $to['Y'] = $from['Y'] = $now['year'] - $subtractYear;
1058 $to['d'] = date('t', mktime(0, 0, 0, $to['M'], 1, $to['Y']));
1063 $quarter = ceil($now['mon'] / 3);
1064 $current_quarter = $quarter;
1065 $quarter = $quarter - $difference;
1067 if ($quarter <= 0) {
1072 $from['M'] = (3 * $quarter) - 2;
1073 switch ($current_quarter) {
1075 $to['M'] = (4 * $quarter);
1079 $to['M'] = (4 * $quarter) +
3;
1083 $to['M'] = (4 * $quarter) +
2;
1087 $to['M'] = (4 * $quarter) +
1;
1090 $to['Y'] = $from['Y'] = $now['year'] - $subtractYear;
1091 if ($to['M'] > 12) {
1092 $to['M'] = 3 * ($quarter - 3);
1093 $to['Y'] = $now['year'];
1095 $to['d'] = date('t', mktime(0, 0, 0, $to['M'], 1, $to['Y']));
1099 $quarter = ceil($now['mon'] / 3) - 1;
1100 if ($quarter <= 0) {
1104 $to['M'] = 3 * $quarter;
1105 $to['Y'] = $from['Y'] = $now['year'] - $subtractYear;
1106 $to['d'] = date('t', mktime(0, 0, 0, $to['M'], 1, $to['Y']));
1111 $quarter = ceil($now['mon'] / 3);
1113 $from['M'] = (3 * $quarter) - 2;
1114 $from['Y'] = $now['year'];
1119 $to['d'] = $now['mday'];
1120 $to['M'] = $now['mon'];
1121 $to['Y'] = $now['year'];
1123 $to['i'] = $to['s'] = 59;
1124 $from = self
::intervalAdd('month', -3, $to);
1125 $from = self
::intervalAdd('second', 1, $from);
1129 $quarter = ceil($now['mon'] / 3);
1131 $from['M'] = (3 * $quarter) - 2;
1132 $from['Y'] = $now['year'];
1133 $to['d'] = $now['mday'];
1134 $to['M'] = $now['mon'];
1135 $to['Y'] = $now['year'];
1137 $to['i'] = $to['s'] = 59;
1143 switch ($relativeTerm) {
1146 $to['d'] = date('t', mktime(0, 0, 0, $now['mon'], 1, $now['year']));
1147 $from['M'] = $to['M'] = $now['mon'];
1148 $from['Y'] = $to['Y'] = $now['year'];
1153 if ($now['mon'] == 1) {
1154 $from['M'] = $to['M'] = 12;
1155 $from['Y'] = $to['Y'] = $now['year'] - 1;
1158 $from['M'] = $to['M'] = $now['mon'] - 1;
1159 $from['Y'] = $to['Y'] = $now['year'];
1161 $to['d'] = date('t', mktime(0, 0, 0, $to['M'], 1, $to['Y']));
1164 case 'previous_before':
1166 if ($now['mon'] < 3) {
1167 $from['M'] = $to['M'] = 10 +
$now['mon'];
1168 $from['Y'] = $to['Y'] = $now['year'] - 1;
1171 $from['M'] = $to['M'] = $now['mon'] - 2;
1172 $from['Y'] = $to['Y'] = $now['year'];
1174 $to['d'] = date('t', mktime(0, 0, 0, $to['M'], 1, $to['Y']));
1179 if ($now['mon'] < 3) {
1180 $from['M'] = 10 +
$now['mon'];
1181 $from['Y'] = $now['year'] - 1;
1184 $from['M'] = $now['mon'] - 2;
1185 $from['Y'] = $now['year'];
1188 if ($now['mon'] == 1) {
1190 $to['Y'] = $now['year'] - 1;
1193 $to['M'] = $now['mon'] - 1;
1194 $to['Y'] = $now['year'];
1197 $to['d'] = date('t', mktime(0, 0, 0, $to['M'], 1, $to['Y']));
1201 //before end of past month
1202 if ($now['mon'] == 1) {
1204 $to['Y'] = $now['year'] - 1;
1207 $to['M'] = $now['mon'] - 1;
1208 $to['Y'] = $now['year'];
1211 $to['d'] = date('t', mktime(0, 0, 0, $to['M'], 1, $to['Y']));
1217 $from['M'] = $now['mon'];;
1218 $from['Y'] = $now['year'];
1223 $to['d'] = $now['mday'];
1224 $to['M'] = $now['mon'];
1225 $to['Y'] = $now['year'];
1227 $to['i'] = $to['s'] = 59;
1228 $from = self
::intervalAdd('month', -1, $to);
1229 $from = self
::intervalAdd('second', 1, $from);
1234 $from['M'] = $now['mon'];;
1235 $from['Y'] = $now['year'];
1236 $to['d'] = $now['mday'];
1237 $to['M'] = $now['mon'];
1238 $to['Y'] = $now['year'];
1240 $to['i'] = $to['s'] = 59;
1246 switch ($relativeTerm) {
1248 $from['d'] = $now['mday'];
1249 $from['M'] = $now['mon'];
1250 $from['Y'] = $now['year'];
1251 $from = self
::intervalAdd('day', -1 * ($now['wday']), $from);
1252 $to = self
::intervalAdd('day', 6, $from);
1256 $from['d'] = $now['mday'];
1257 $from['M'] = $now['mon'];
1258 $from['Y'] = $now['year'];
1259 $from = self
::intervalAdd('day', -1 * ($now['wday']) - 7, $from);
1260 $to = self
::intervalAdd('day', 6, $from);
1263 case 'previous_before':
1264 $from['d'] = $now['mday'];
1265 $from['M'] = $now['mon'];
1266 $from['Y'] = $now['year'];
1267 $from = self
::intervalAdd('day', -1 * ($now['wday']) - 14, $from);
1268 $to = self
::intervalAdd('day', 6, $from);
1272 $from['d'] = $now['mday'];
1273 $from['M'] = $now['mon'];
1274 $from['Y'] = $now['year'];
1275 $from = self
::intervalAdd('day', -1 * ($now['wday']) - 14, $from);
1276 $to = self
::intervalAdd('day', 13, $from);
1280 $to['d'] = $now['mday'];
1281 $to['M'] = $now['mon'];
1282 $to['Y'] = $now['year'];
1283 $to = self
::intervalAdd('day', -1 * ($now['wday']) - 1, $to);
1288 $from['d'] = $now['mday'];
1289 $from['M'] = $now['mon'];
1290 $from['Y'] = $now['year'];
1291 $from = self
::intervalAdd('day', -1 * ($now['wday']), $from);
1296 $to['d'] = $now['mday'];
1297 $to['M'] = $now['mon'];
1298 $to['Y'] = $now['year'];
1300 $to['i'] = $to['s'] = 59;
1301 $from = self
::intervalAdd('day', -7, $to);
1302 $from = self
::intervalAdd('second', 1, $from);
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['d'] = $now['mday'];
1311 $to['M'] = $now['mon'];
1312 $to['Y'] = $now['year'];
1314 $to['i'] = $to['s'] = 59;
1320 switch ($relativeTerm) {
1322 $from['d'] = $to['d'] = $now['mday'];
1323 $from['M'] = $to['M'] = $now['mon'];
1324 $from['Y'] = $to['Y'] = $now['year'];
1328 $from['d'] = $now['mday'];
1329 $from['M'] = $now['mon'];
1330 $from['Y'] = $now['year'];
1331 $from = self
::intervalAdd('day', -1, $from);
1332 $to['d'] = $from['d'];
1333 $to['M'] = $from['M'];
1334 $to['Y'] = $from['Y'];
1337 case 'previous_before':
1338 $from['d'] = $now['mday'];
1339 $from['M'] = $now['mon'];
1340 $from['Y'] = $now['year'];
1341 $from = self
::intervalAdd('day', -2, $from);
1342 $to['d'] = $from['d'];
1343 $to['M'] = $from['M'];
1344 $to['Y'] = $from['Y'];
1348 $from['d'] = $to['d'] = $now['mday'];
1349 $from['M'] = $to['M'] = $now['mon'];
1350 $from['Y'] = $to['Y'] = $now['year'];
1351 $from = self
::intervalAdd('day', -2, $from);
1352 $to = self
::intervalAdd('day', -1, $to);
1356 $to['d'] = $now['mday'];
1357 $to['M'] = $now['mon'];
1358 $to['Y'] = $now['year'];
1363 $from['d'] = $now['mday'];
1364 $from['M'] = $now['mon'];;
1365 $from['Y'] = $now['year'];
1373 'from', 'to') as $item) {
1374 if (!empty($
$item)) {
1375 $dateRange[$item] = self
::format($
$item);
1378 $dateRange[$item] = NULL;
1385 * Function to calculate current fiscal year based on the fiscal month and day
1387 * @param int $fyDate Fiscal start date
1389 * @param int $fyMonth Fiscal Start Month
1391 * @return int $fy Current Fiscl Year
1395 static function calculateFiscalYear($fyDate, $fyMonth) {
1396 $date = date("Y-m-d");
1397 $currentYear = date("Y");
1399 //recalculate the date because month 4::04 make the difference
1400 $fiscalYear = explode('-', date("Y-m-d", mktime(0, 0, 0, $fyMonth, $fyDate, $currentYear)));
1401 $fyDate = $fiscalYear[2];
1402 $fyMonth = $fiscalYear[1];
1403 $fyStartDate = date("Y-m-d", mktime(0, 0, 0, $fyMonth, $fyDate, $currentYear));
1405 if ($fyStartDate > $date) {
1406 $fy = intval(intval($currentYear) - 1);
1409 $fy = intval($currentYear);
1415 * Function to process date, convert to mysql format
1417 * @param string $date date string
1418 * @param string $time time string
1419 * @param string $returnNullString 'null' needs to be returned
1420 * so that db oject will set null in db
1421 * @param string $format expected return date format.( default is mysql )
1423 * @return string $mysqlDate date format that is excepted by mysql
1425 static function processDate($date, $time = NULL, $returnNullString = FALSE, $format = 'YmdHis') {
1428 if ($returnNullString) {
1429 $mysqlDate = 'null';
1433 $mysqlDate = date($format, strtotime($date . ' ' . $time));
1440 * Function to convert mysql to date plugin format
1442 * @param string $mysqlDate date string
1444 * @return array $date and time
1446 static function setDateDefaults($mysqlDate = NULL, $formatType = NULL, $format = NULL, $timeFormat = NULL) {
1447 // if date is not passed assume it as today
1449 $mysqlDate = date('Y-m-d G:i:s');
1452 $config = CRM_Core_Config
::singleton();
1454 // get actual format
1455 $params = array('name' => $formatType);
1457 CRM_Core_DAO
::commonRetrieve('CRM_Core_DAO_PreferencesDate', $params, $values);
1459 if ($values['date_format']) {
1460 $format = $values['date_format'];
1463 if (isset($values['time_format'])) {
1464 $timeFormat = $values['time_format'];
1468 // now we set display date using js, hence we should always setdefault
1469 // 'm/d/Y' format. So that submitted value is alwats mm/dd/YY format
1470 // note that for date display we dynamically create text field
1473 $format = $config->dateInputFormat;
1476 // get actual format
1477 $actualPHPFormats = CRM_Core_SelectValues::datePluginToPHPFormats( );
1478 $dateFormat = CRM_Utils_Array::value( $format, $actualPHPFormats );
1482 $dateFormat = 'm/d/Y';
1483 $date = date($dateFormat, strtotime($mysqlDate));
1486 $timeFormat = $config->timeInputFormat
;
1489 $actualTimeFormat = "g:iA";
1490 $appendZeroLength = 7;
1491 if ($timeFormat > 1) {
1492 $actualTimeFormat = "G:i";
1493 $appendZeroLength = 5;
1496 $time = date($actualTimeFormat, strtotime($mysqlDate));
1498 // need to append zero for hours < 10
1499 if (strlen($time) < $appendZeroLength) {
1500 $time = '0' . $time;
1503 return array($date, $time);
1507 * Function get date format
1509 * @param string $formatType Date name e.g. birth
1511 * @return string $format
1513 static function getDateFormat($formatType = NULL) {
1516 $format = CRM_Core_Dao
::getFieldValue('CRM_Core_DAO_PreferencesDate',
1517 $formatType, 'date_format', 'name'
1522 $config = CRM_Core_Config
::singleton();
1523 $format = $config->dateInputFormat
;
1529 * Get the time in UTC for the current time. You can optionally send an offset from the current time if needed
1531 * @param $offset int the offset from the current time in seconds
1533 * @return the time in UTC
1537 static function getUTCTime($offset = 0) {
1538 $originalTimezone = date_default_timezone_get();
1539 date_default_timezone_set('UTC');
1540 $time = time() +
$offset;
1541 $now = date('YmdHis', $time);
1542 date_default_timezone_set($originalTimezone);
1547 static function formatDate($date, $dateType) {
1548 $formattedDate = NULL;
1550 return $formattedDate;
1553 //1. first convert date to default format.
1554 //2. append time to default formatted date (might be removed during format)
1555 //3. validate date / date time.
1556 //4. If date and time then convert to default date time format.
1559 $dateParams = array($dateKey => $date);
1561 if (CRM_Utils_Date
::convertToDefaultDate($dateParams, $dateType, $dateKey)) {
1562 $dateVal = $dateParams[$dateKey];
1564 if ($dateType == 1) {
1566 if (preg_match("/(\s(([01]\d)|[2][0-3]):([0-5]\d))$/", $date, $matches)) {
1567 $ruleName = 'dateTime';
1568 if (strpos($date, '-') !== FALSE) {
1569 $dateVal .= array_shift($matches);
1575 $valid = CRM_Utils_Rule
::$ruleName($dateVal);
1578 //format date and time to default.
1579 if ($ruleName == 'dateTime') {
1580 $dateVal = CRM_Utils_Date
::customFormat(preg_replace("/(:|\s)?/", "", $dateVal), '%Y%m%d%H%i');
1581 //hack to add seconds
1584 $formattedDate = $dateVal;
1588 return $formattedDate;