3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
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'.
46 * @param string $separator
47 * The seperator to use when formatting the date.
48 * @param int|string $invalidDate what to return if the date is invalid
51 * formatted string for date
54 public static function format($date, $separator = '', $invalidDate = 0) {
55 if (is_numeric($date) &&
56 ((strlen($date) == 8) ||
(strlen($date) == 14))
61 if (!is_array($date) ||
62 CRM_Utils_System
::isNull($date) ||
68 $date['Y'] = (int ) $date['Y'];
69 if ($date['Y'] < 1000 ||
$date['Y'] > 2999) {
73 if (array_key_exists('m', $date)) {
74 $date['M'] = $date['m'];
76 elseif (array_key_exists('F', $date)) {
77 $date['M'] = $date['F'];
80 if (!empty($date['M'])) {
81 $date['M'] = (int ) $date['M'];
82 if ($date['M'] < 1 ||
$date['M'] > 12) {
90 if (!empty($date['d'])) {
91 $date['d'] = (int ) $date['d'];
97 if (!checkdate($date['M'], $date['d'], $date['Y'])) {
101 $date['M'] = sprintf('%02d', $date['M']);
102 $date['d'] = sprintf('%02d', $date['d']);
105 if (CRM_Utils_Array
::value('H', $date) != NULL ||
106 CRM_Utils_Array
::value('h', $date) != NULL ||
107 CRM_Utils_Array
::value('i', $date) != NULL ||
108 CRM_Utils_Array
::value('s', $date) != NULL
110 // we have time too..
111 if (!empty($date['h'])) {
112 if (CRM_Utils_Array
::value('A', $date) == 'PM' or CRM_Utils_Array
::value('a', $date) == 'pm') {
113 if ($date['h'] != 12) {
114 $date['h'] = $date['h'] +
12;
117 if ((CRM_Utils_Array
::value('A', $date) == 'AM' or CRM_Utils_Array
::value('a', $date) == 'am') &&
118 CRM_Utils_Array
::value('h', $date) == 12
123 $date['h'] = (int ) $date['h'];
129 // in 24-hour format the hour is under the 'H' key
130 if (!empty($date['H'])) {
131 $date['H'] = (int) $date['H'];
137 if (!empty($date['i'])) {
138 $date['i'] = (int ) $date['i'];
144 if ($date['h'] == 0 && $date['H'] != 0) {
145 $date['h'] = $date['H'];
148 if (!empty($date['s'])) {
149 $date['s'] = (int ) $date['s'];
155 $date['h'] = sprintf('%02d', $date['h']);
156 $date['i'] = sprintf('%02d', $date['i']);
157 $date['s'] = sprintf('%02d', $date['s']);
162 $time .= $date['h'] . $separator . $date['i'] . $separator . $date['s'];
165 return $date['Y'] . $separator . $date['M'] . $separator . $date['d'] . $time;
169 * Return abbreviated weekday names according to the locale.
172 * 0-based array with abbreviated weekday names
175 public static function &getAbbrWeekdayNames() {
176 static $abbrWeekdayNames;
177 if (!isset($abbrWeekdayNames)) {
179 // set LC_TIME and build the arrays from locale-provided names
180 // June 1st, 1970 was a Monday
181 CRM_Core_I18n
::setLcTime();
182 for ($i = 0; $i < 7; $i++
) {
183 $abbrWeekdayNames[$i] = strftime('%a', mktime(0, 0, 0, 6, $i, 1970));
186 return $abbrWeekdayNames;
190 * Return full weekday names according to the locale.
193 * 0-based array with full weekday names
196 public static function &getFullWeekdayNames() {
197 static $fullWeekdayNames;
198 if (!isset($fullWeekdayNames)) {
200 // set LC_TIME and build the arrays from locale-provided names
201 // June 1st, 1970 was a Monday
202 CRM_Core_I18n
::setLcTime();
203 for ($i = 0; $i < 7; $i++
) {
204 $fullWeekdayNames[$i] = strftime('%A', mktime(0, 0, 0, 6, $i, 1970));
207 return $fullWeekdayNames;
211 * Return abbreviated month names according to the locale.
216 * 1-based array with abbreviated month names
219 public static function &getAbbrMonthNames($month = FALSE) {
220 static $abbrMonthNames;
221 if (!isset($abbrMonthNames)) {
223 // set LC_TIME and build the arrays from locale-provided names
224 CRM_Core_I18n
::setLcTime();
225 for ($i = 1; $i <= 12; $i++
) {
226 $abbrMonthNames[$i] = strftime('%b', mktime(0, 0, 0, $i, 10, 1970));
230 return $abbrMonthNames[$month];
232 return $abbrMonthNames;
236 * Return full month names according to the locale.
239 * 1-based array with full month names
242 public static function &getFullMonthNames() {
243 static $fullMonthNames;
244 if (!isset($fullMonthNames)) {
246 // set LC_TIME and build the arrays from locale-provided names
247 CRM_Core_I18n
::setLcTime();
248 for ($i = 1; $i <= 12; $i++
) {
249 $fullMonthNames[$i] = strftime('%B', mktime(0, 0, 0, $i, 10, 1970));
252 return $fullMonthNames;
260 public static function unixTime($string) {
261 if (empty($string)) {
264 $parsedDate = date_parse($string);
265 return mktime(CRM_Utils_Array
::value('hour', $parsedDate),
266 CRM_Utils_Array
::value('minute', $parsedDate),
268 CRM_Utils_Array
::value('month', $parsedDate),
269 CRM_Utils_Array
::value('day', $parsedDate),
270 CRM_Utils_Array
::value('year', $parsedDate)
275 * Create a date and time string in a provided format.
277 * %b - abbreviated month name ('Jan'..'Dec')
278 * %B - full month name ('January'..'December')
279 * %d - day of the month as a decimal number, 0-padded ('01'..'31')
280 * %e - day of the month as a decimal number, blank-padded (' 1'..'31')
281 * %E - day of the month as a decimal number ('1'..'31')
282 * %f - English ordinal suffix for the day of the month ('st', 'nd', 'rd', 'th')
283 * %H - hour in 24-hour format, 0-padded ('00'..'23')
284 * %I - hour in 12-hour format, 0-padded ('01'..'12')
285 * %k - hour in 24-hour format, blank-padded (' 0'..'23')
286 * %l - hour in 12-hour format, blank-padded (' 1'..'12')
287 * %m - month as a decimal number, 0-padded ('01'..'12')
288 * %M - minute, 0-padded ('00'..'60')
289 * %p - lowercase ante/post meridiem ('am', 'pm')
290 * %P - uppercase ante/post meridiem ('AM', 'PM')
291 * %Y - year as a decimal number including the century ('2005')
293 * @param string $dateString
294 * Date and time in 'YYYY-MM-DD hh:mm:ss' format.
295 * @param string $format
297 * @param array $dateParts
298 * An array with the desired date parts.
301 * the $format-formatted $date
303 public static function customFormat($dateString, $format = NULL, $dateParts = NULL) {
304 // 1-based (January) month names arrays
305 $abbrMonths = self
::getAbbrMonthNames();
306 $fullMonths = self
::getFullMonthNames();
309 $config = CRM_Core_Config
::singleton();
312 if (array_intersect(array('h', 'H'), $dateParts)) {
313 $format = $config->dateformatDatetime
;
315 elseif (array_intersect(array('d', 'j'), $dateParts)) {
316 $format = $config->dateformatFull
;
318 elseif (array_intersect(array('m', 'M'), $dateParts)) {
319 $format = $config->dateformatPartial
;
322 $format = $config->dateformatYear
;
326 if (strpos($dateString, '-')) {
327 $month = (int) substr($dateString, 5, 2);
328 $day = (int) substr($dateString, 8, 2);
331 $month = (int) substr($dateString, 4, 2);
332 $day = (int) substr($dateString, 6, 2);
335 if (strlen($dateString) > 10) {
336 $format = $config->dateformatDatetime
;
339 $format = $config->dateformatFull
;
341 elseif ($month > 0) {
342 $format = $config->dateformatPartial
;
345 $format = $config->dateformatYear
;
350 if (!CRM_Utils_System
::isNull($dateString)) {
351 if (strpos($dateString, '-')) {
352 $year = (int) substr($dateString, 0, 4);
353 $month = (int) substr($dateString, 5, 2);
354 $day = (int) substr($dateString, 8, 2);
356 $hour24 = (int) substr($dateString, 11, 2);
357 $minute = (int) substr($dateString, 14, 2);
360 $year = (int) substr($dateString, 0, 4);
361 $month = (int) substr($dateString, 4, 2);
362 $day = (int) substr($dateString, 6, 2);
364 $hour24 = (int) substr($dateString, 8, 2);
365 $minute = (int) substr($dateString, 10, 2);
368 if ($day %
10 == 1 and $day != 11) {
371 elseif ($day %
10 == 2 and $day != 12) {
374 elseif ($day %
10 == 3 and $day != 13) {
395 $hour12 = $hour24 - 12;
401 '%b' => CRM_Utils_Array
::value($month, $abbrMonths),
402 '%B' => CRM_Utils_Array
::value($month, $fullMonths),
403 '%d' => $day > 9 ?
$day : '0' . $day,
404 '%e' => $day > 9 ?
$day : ' ' . $day,
407 '%H' => $hour24 > 9 ?
$hour24 : '0' . $hour24,
408 '%h' => $hour12 > 9 ?
$hour12 : '0' . $hour12,
409 '%I' => $hour12 > 9 ?
$hour12 : '0' . $hour12,
410 '%k' => $hour24 > 9 ?
$hour24 : ' ' . $hour24,
411 '%l' => $hour12 > 9 ?
$hour12 : ' ' . $hour12,
412 '%m' => $month > 9 ?
$month : '0' . $month,
413 '%M' => $minute > 9 ?
$minute : '0' . $minute,
414 '%i' => $minute > 9 ?
$minute : '0' . $minute,
415 '%p' => strtolower($type),
421 return strtr($format, $date);
429 * Converts the date/datetime from MySQL format to ISO format
431 * @param string $mysql
432 * Date/datetime in MySQL format.
435 * date/datetime in ISO format
437 public static function mysqlToIso($mysql) {
438 $year = substr($mysql, 0, 4);
439 $month = substr($mysql, 4, 2);
440 $day = substr($mysql, 6, 2);
441 $hour = substr($mysql, 8, 2);
442 $minute = substr($mysql, 10, 2);
443 $second = substr($mysql, 12, 2);
469 * Converts the date/datetime from ISO format to MySQL format
470 * Note that until CRM-14986/ 4.4.7 this was required whenever the pattern $dao->find(TRUE): $dao->save(); was
471 * used to update an object with a date field was used. The DAO now checks for a '-' in date field strings
472 * & runs this function if the - appears - meaning it is likely redundant in the form & BAO layers
475 * Date/datetime in ISO format.
478 * date/datetime in MySQL format
480 public static function isoToMysql($iso) {
481 $dropArray = array('-' => '', ':' => '', ' ' => '');
482 return strtr($iso, $dropArray);
486 * Converts the any given date to default date format.
488 * @param array $params
489 * Has given date-format.
490 * @param int $dateType
492 * @param string $dateParam
497 public static function convertToDefaultDate(&$params, $dateType, $dateParam) {
499 $cen = substr($now['year'], 0, 2);
503 if (!empty($params[$dateParam])) {
504 // suppress hh:mm or hh:mm:ss if it exists CRM-7957
505 $value = preg_replace("/(\s(([01]\d)|[2][0-3])(:([0-5]\d)){1,2})$/", "", $params[$dateParam]);
510 if (!preg_match('/^\d\d\d\d-?(\d|\d\d)-?(\d|\d\d)$/', $value)) {
516 if (!preg_match('/^(\d|\d\d)[-\/](\d|\d\d)[-\/]\d\d$/', $value)) {
522 if (!preg_match('/^(\d|\d\d)[-\/](\d|\d\d)[-\/]\d\d\d\d$/', $value)) {
528 if (!preg_match('/^[A-Za-z]*.[ \t]?\d\d\,[ \t]?\d\d\d\d$/', $value)) {
534 if (!preg_match('/^\d\d-[A-Za-z]{3}.*-\d\d$/', $value) && !preg_match('/^\d\d[-\/]\d\d[-\/]\d\d$/', $value)) {
540 if (!preg_match('/^(\d|\d\d)[-\/](\d|\d\d)[-\/]\d\d\d\d/', $value)) {
546 if ($dateType == 1) {
547 $formattedDate = explode("-", $value);
548 if (count($formattedDate) == 3) {
549 $year = (int) $formattedDate[0];
550 $month = (int) $formattedDate[1];
551 $day = (int) $formattedDate[2];
553 elseif (count($formattedDate) == 1 && (strlen($value) == 8)) {
561 if ($dateType == 2 ||
$dateType == 4) {
562 $formattedDate = explode("/", $value);
563 if (count($formattedDate) != 3) {
564 $formattedDate = explode("-", $value);
566 if (count($formattedDate) == 3) {
567 $year = (int) $formattedDate[2];
568 $month = (int) $formattedDate[0];
569 $day = (int) $formattedDate[1];
575 if ($dateType == 8) {
576 $dateArray = explode(' ', $value);
578 $dateArray[1] = (int) substr($dateArray[1], 0, 2);
581 $fullMonths = self
::getFullMonthNames();
582 foreach ($fullMonths as $key => $val) {
583 if (strtolower($dateArray[0]) == strtolower($val)) {
589 $abbrMonths = self
::getAbbrMonthNames();
590 foreach ($abbrMonths as $key => $val) {
591 if (strtolower(trim($dateArray[0], ".")) == strtolower($val)) {
597 $year = (int) $dateArray[2];
598 $day = (int) $dateArray[1];
599 $month = (int) $monthInt;
601 if ($dateType == 16) {
602 $dateArray = explode('-', $value);
603 if (count($dateArray) != 3) {
604 $dateArray = explode('/', $value);
607 if (count($dateArray) == 3) {
609 $fullMonths = self
::getFullMonthNames();
610 foreach ($fullMonths as $key => $val) {
611 if (strtolower($dateArray[1]) == strtolower($val)) {
617 $abbrMonths = self
::getAbbrMonthNames();
618 foreach ($abbrMonths as $key => $val) {
619 if (strtolower(trim($dateArray[1], ".")) == strtolower($val)) {
626 $monthInt = $dateArray[1];
629 $year = (int) $dateArray[2];
630 $day = (int) $dateArray[0];
631 $month = (int) $monthInt;
637 if ($dateType == 32) {
638 $formattedDate = explode("/", $value);
639 if (count($formattedDate) == 3) {
640 $year = (int) $formattedDate[2];
641 $month = (int) $formattedDate[1];
642 $day = (int) $formattedDate[0];
649 $month = ($month < 10) ?
"0" . "$month" : $month;
650 $day = ($day < 10) ?
"0" . "$day" : $day;
652 $year = (int ) $year;
653 // simple heuristic to determine what century to use
654 // 00 - 20 is always 2000 - 2020
655 // 21 - 99 is always 1921 - 1999
657 $year = (strlen($year) == 1) ?
$cen . '0' . $year : $cen . $year;
659 elseif ($year < 100) {
660 $year = $prevCen . $year;
663 if ($params[$dateParam]) {
664 $params[$dateParam] = "$year$month$day";
666 //if month is invalid return as error
667 if ($month !== '00' && $month <= 12) {
678 public static function isDate(&$date) {
679 if (CRM_Utils_System
::isNull($date)) {
686 * @param null $timeStamp
688 * @return bool|string
690 public static function currentDBDate($timeStamp = NULL) {
691 return $timeStamp ?
date('YmdHis', $timeStamp) : date('YmdHis');
700 public static function overdue($date, $now = NULL) {
701 $mysqlDate = self
::isoToMysql($date);
703 $now = self
::currentDBDate();
706 $now = self
::isoToMysql($now);
709 return ($mysqlDate >= $now) ?
FALSE : TRUE;
713 * Get customized today.
715 * This function is used for getting customized today. To get
716 * actuall today pass 'dayParams' as null. or else pass the day,
717 * month, year values as array values
718 * Example: $dayParams = array(
719 * 'day' => '25', 'month' => '10',
720 * 'year' => '2007' );
722 * @param array $dayParams of the day, month, year.
723 * Array of the day, month, year.
725 * @param string $format
726 * Expected date format( default.
727 * format is 2007-12-21 )
730 * Return the customized todays date (Y-m-d)
732 public static function getToday($dayParams = NULL, $format = "Y-m-d") {
733 if (is_null($dayParams) ||
empty($dayParams)) {
734 $today = date($format);
737 $today = date($format, mktime(0, 0, 0,
748 * Find whether today's date lies in
751 * @param date $startDate
752 * Start date for the range.
753 * @param date $endDate
754 * End date for the range.
757 * true if today's date is in the given date range
759 public static function getRange($startDate, $endDate) {
760 $today = date("Y-m-d");
761 $mysqlStartDate = self
::isoToMysql($startDate);
762 $mysqlEndDate = self
::isoToMysql($endDate);
763 $mysqlToday = self
::isoToMysql($today);
765 if ((isset($mysqlStartDate) && isset($mysqlEndDate)) && (($mysqlToday >= $mysqlStartDate) && ($mysqlToday <= $mysqlEndDate))) {
768 elseif ((isset($mysqlStartDate) && !isset($mysqlEndDate)) && (($mysqlToday >= $mysqlStartDate))) {
771 elseif ((!isset($mysqlStartDate) && isset($mysqlEndDate)) && (($mysqlToday <= $mysqlEndDate))) {
778 * Get start date and end from
779 * the given relative term and unit
781 * @param date $relative
788 * start date, end date
790 public static function getFromTo($relative, $from, $to) {
792 list($term, $unit) = explode('.', $relative);
793 $dateRange = self
::relativeToAbsolute($term, $unit);
794 $from = $dateRange['from'];
795 //Take only Date Part, Sometime Time part is also present in 'to'
796 $to = substr($dateRange['to'], 0, 8);
799 $from = self
::processDate($from);
800 $to = self
::processDate($to, '235959');
802 return array($from, $to);
806 * Calculate Age in Years if greater than one year else in months.
808 * @param date $birthDate
812 * array $results contains years or months
814 static public function calculateAge($birthDate) {
816 $formatedBirthDate = CRM_Utils_Date
::customFormat($birthDate, '%Y-%m-%d');
818 $bDate = explode('-', $formatedBirthDate);
819 $birthYear = $bDate[0];
820 $birthMonth = $bDate[1];
821 $birthDay = $bDate[2];
822 $year_diff = date("Y") - $birthYear;
824 // don't calculate age CRM-3143
825 if ($birthYear == '1902') {
829 switch ($year_diff) {
831 $month = (12 - $birthMonth) +
date("m");
833 if (date("d") < $birthDay) {
836 $results['months'] = $month;
838 elseif ($month == 12 && (date("d") < $birthDay)) {
839 $results['months'] = $month - 1;
842 $results['years'] = $year_diff;
847 $month = date("m") - $birthMonth;
848 $results['months'] = $month;
852 $results['years'] = $year_diff;
853 if ((date("m") < $birthMonth) ||
(date("m") == $birthMonth) && (date("d") < $birthDay)) {
862 * Calculate next payment date according to provided unit & interval
864 * @param string $unit
865 * Frequency unit like year,month, week etc.
867 * @param int $interval
868 * Frequency interval.
871 * Start date of pledge.
873 * @param bool $dontCareTime
876 * contains new date with added interval
878 public static function intervalAdd($unit, $interval, $date, $dontCareTime = FALSE) {
879 if (is_array($date)) {
880 $hour = CRM_Utils_Array
::value('H', $date);
881 $minute = CRM_Utils_Array
::value('i', $date);
882 $second = CRM_Utils_Array
::value('s', $date);
883 $month = CRM_Utils_Array
::value('M', $date);
884 $day = CRM_Utils_Array
::value('d', $date);
885 $year = CRM_Utils_Array
::value('Y', $date);
888 extract(date_parse($date));
890 $date = mktime($hour, $minute, $second, $month, $day, $year);
893 $date = mktime($hour, $minute, $second, $month, $day, $year +
$interval);
897 $date = mktime($hour, $minute, $second, $month +
$interval, $day, $year);
901 $interval = $interval * 7;
902 $date = mktime($hour, $minute, $second, $month, $day +
$interval, $year);
906 $date = mktime($hour, $minute, $second, $month, $day +
$interval, $year);
910 $date = mktime($hour, $minute, $second +
$interval, $month, $day, $year);
914 $scheduleDate = explode("-", date("n-j-Y-H-i-s", $date));
917 $date['M'] = $scheduleDate[0];
918 $date['d'] = $scheduleDate[1];
919 $date['Y'] = $scheduleDate[2];
920 if ($dontCareTime == FALSE) {
921 $date['H'] = $scheduleDate[3];
922 $date['i'] = $scheduleDate[4];
923 $date['s'] = $scheduleDate[5];
929 * Check given format is valid for bith date.
930 * and retrun supportable birth date format w/ qf mapping.
933 * Given format ( eg 'M Y', 'Y M' ).
934 * return array of qfMapping and date parts for date format.
936 * @return array|null|string
938 public static function &checkBirthDateFormat($format = NULL) {
939 $birthDateFormat = NULL;
941 $birthDateFormat = self
::getDateFormat('birth');
944 $supportableFormats = array(
945 'mm/dd' => '%B %E%f',
946 'dd-mm' => '%E%f %B',
950 'dd/mm/yy' => '%E%f %B %Y',
953 if (array_key_exists($birthDateFormat, $supportableFormats)) {
954 $birthDateFormat = array('qfMapping' => $supportableFormats[$birthDateFormat]);
957 return $birthDateFormat;
961 * Resolves the given relative time interval into finite time limits.
963 * @param array $relativeTerm
964 * Relative time frame like this, previous, etc.
966 * Frequency unit like year, month, week etc.
969 * start date and end date for the relative time frame
971 public static function relativeToAbsolute($relativeTerm, $unit) {
973 $from = $to = $dateRange = array();
974 $from['H'] = $from['i'] = $from['s'] = 0;
978 switch ($relativeTerm) {
980 $from['d'] = $from['M'] = 1;
983 $to['Y'] = $from['Y'] = $now['year'];
987 $from['M'] = $from['d'] = 1;
990 $to['Y'] = $from['Y'] = $now['year'] - 1;
993 case 'previous_before':
994 $from['M'] = $from['d'] = 1;
997 $to['Y'] = $from['Y'] = $now['year'] - 2;
1001 $from['M'] = $from['d'] = 1;
1004 $from['Y'] = $now['year'] - 2;
1005 $to['Y'] = $now['year'] - 1;
1011 $to['Y'] = $now['year'] - 1;
1016 $from['M'] = $from['d'] = 1;
1017 $from['Y'] = $now['year'];
1021 case 'greater_previous':
1024 $from['Y'] = $now['year'] - 1;
1029 $to['d'] = $now['mday'];
1030 $to['M'] = $now['mon'];
1031 $to['Y'] = $now['year'];
1033 $to['i'] = $to['s'] = 59;
1034 $from = self
::intervalAdd('year', -1, $to);
1035 $from = self
::intervalAdd('second', 1, $from);
1039 $from['M'] = $from['d'] = 1;
1040 $from['Y'] = $now['year'];
1042 $to['i'] = $to['s'] = 59;
1043 $to['d'] = $now['mday'];
1044 $to['M'] = $now['mon'];
1045 $to['Y'] = $now['year'];
1049 $to['d'] = $now['mday'];
1050 $to['M'] = $now['mon'];
1051 $to['Y'] = $now['year'];
1053 $to['i'] = $to['s'] = 59;
1054 $from = self
::intervalAdd('year', -2, $to);
1055 $from = self
::intervalAdd('second', 1, $from);
1059 $to['d'] = $now['mday'];
1060 $to['M'] = $now['mon'];
1061 $to['Y'] = $now['year'];
1063 $to['i'] = $to['s'] = 59;
1064 $from = self
::intervalAdd('year', -3, $to);
1065 $from = self
::intervalAdd('second', 1, $from);
1071 $to['Y'] = $now['year'];
1076 $from['M'] = $from['d'] = 1;
1079 $to['Y'] = $from['Y'] = $now['year'] +
1;
1083 $from['d'] = $now['mday'];
1084 $from['M'] = $now['mon'];
1085 $from['Y'] = $now['year'];
1086 $to['d'] = $now['mday'] - 1;
1087 $to['M'] = $now['mon'];
1088 $to['Y'] = $now['year'] +
1;
1094 $config = CRM_Core_Config
::singleton();
1095 $from['d'] = $config->fiscalYearStart
['d'];
1096 $from['M'] = $config->fiscalYearStart
['M'];
1097 $fYear = self
::calculateFiscalYear($from['d'], $from['M']);
1098 switch ($relativeTerm) {
1100 $from['Y'] = $fYear;
1101 $fiscalYear = mktime(0, 0, 0, $from['M'], $form['d'], $from['Y'] +
1);
1102 $fiscalEnd = explode('-', date("Y-m-d", $fiscalYear));
1104 $to['d'] = $fiscalEnd['2'];
1105 $to['M'] = $fiscalEnd['1'];
1106 $to['Y'] = $fiscalEnd['0'];
1110 $from['Y'] = $fYear - 1;
1111 $fiscalYear = mktime(0, 0, 0, $from['M'], $form['d'], $from['Y'] +
1);
1112 $fiscalEnd = explode('-', date("Y-m-d", $fiscalYear));
1113 $to['d'] = $fiscalEnd['2'];
1114 $to['M'] = $fiscalEnd['1'];
1115 $to['Y'] = $fiscalEnd['0'];
1119 $from['Y'] = $fYear +
1;
1120 $fiscalYear = mktime(0, 0, 0, $from['M'], $from['d'], $from['Y'] +
1);
1121 $fiscalEnd = explode('-', date("Y-m-d", $fiscalYear));
1122 $to['d'] = $fiscalEnd['2'];
1123 $to['M'] = $fiscalEnd['1'];
1124 $to['Y'] = $fiscalEnd['0'];
1130 switch ($relativeTerm) {
1133 $quarter = ceil($now['mon'] / 3);
1135 $from['M'] = (3 * $quarter) - 2;
1136 $to['M'] = 3 * $quarter;
1137 $to['Y'] = $from['Y'] = $now['year'];
1138 $to['d'] = date('t', mktime(0, 0, 0, $to['M'], 1, $now['year']));
1143 $quarter = ceil($now['mon'] / 3);
1144 $quarter = $quarter - $difference;
1146 if ($quarter <= 0) {
1151 $from['M'] = (3 * $quarter) - 2;
1152 $to['M'] = 3 * $quarter;
1153 $to['Y'] = $from['Y'] = $now['year'] - $subtractYear;
1154 $to['d'] = date('t', mktime(0, 0, 0, $to['M'], 1, $to['Y']));
1157 case 'previous_before':
1159 $quarter = ceil($now['mon'] / 3);
1160 $quarter = $quarter - $difference;
1162 if ($quarter <= 0) {
1167 $from['M'] = (3 * $quarter) - 2;
1168 $to['M'] = 3 * $quarter;
1169 $to['Y'] = $from['Y'] = $now['year'] - $subtractYear;
1170 $to['d'] = date('t', mktime(0, 0, 0, $to['M'], 1, $to['Y']));
1175 $quarter = ceil($now['mon'] / 3);
1176 $current_quarter = $quarter;
1177 $quarter = $quarter - $difference;
1179 if ($quarter <= 0) {
1184 $from['M'] = (3 * $quarter) - 2;
1185 switch ($current_quarter) {
1187 $to['M'] = (4 * $quarter);
1191 $to['M'] = (4 * $quarter) +
3;
1195 $to['M'] = (4 * $quarter) +
2;
1199 $to['M'] = (4 * $quarter) +
1;
1202 $to['Y'] = $from['Y'] = $now['year'] - $subtractYear;
1203 if ($to['M'] > 12) {
1204 $to['M'] = 3 * ($quarter - 3);
1205 $to['Y'] = $now['year'];
1207 $to['d'] = date('t', mktime(0, 0, 0, $to['M'], 1, $to['Y']));
1211 $quarter = ceil($now['mon'] / 3) - 1;
1213 if ($quarter <= 0) {
1217 $to['M'] = 3 * $quarter;
1218 $to['Y'] = $from['Y'] = $now['year'] - $subtractYear;
1219 $to['d'] = date('t', mktime(0, 0, 0, $to['M'], 1, $to['Y']));
1224 $quarter = ceil($now['mon'] / 3);
1226 $from['M'] = (3 * $quarter) - 2;
1227 $from['Y'] = $now['year'];
1231 case 'greater_previous':
1232 $quarter = ceil($now['mon'] / 3) - 1;
1234 if ($quarter <= 0) {
1238 $from['M'] = 3 * $quarter;
1239 $from['Y'] = $from['Y'] = $now['year'] - $subtractYear;
1240 $from['d'] = date('t', mktime(0, 0, 0, $from['M'], 1, $from['Y']));
1245 $to['d'] = $now['mday'];
1246 $to['M'] = $now['mon'];
1247 $to['Y'] = $now['year'];
1249 $to['i'] = $to['s'] = 59;
1250 $from = self
::intervalAdd('month', -3, $to);
1251 $from = self
::intervalAdd('second', 1, $from);
1255 $quarter = ceil($now['mon'] / 3);
1257 $from['M'] = (3 * $quarter) - 2;
1258 $from['Y'] = $now['year'];
1259 $to['d'] = $now['mday'];
1260 $to['M'] = $now['mon'];
1261 $to['Y'] = $now['year'];
1263 $to['i'] = $to['s'] = 59;
1267 $quarter = ceil($now['mon'] / 3);
1268 $to['M'] = 3 * $quarter;
1269 $to['Y'] = $now['year'];
1270 $to['d'] = date('t', mktime(0, 0, 0, $to['M'], 1, $now['year']));
1277 $quarter = ceil($now['mon'] / 3);
1278 $quarter = $quarter - $difference;
1281 $now['year'] = $now['year'] +
1;
1284 if ($quarter <= 0) {
1289 $from['M'] = (3 * $quarter) - 2;
1290 $to['M'] = 3 * $quarter;
1291 $to['Y'] = $from['Y'] = $now['year'] - $subtractYear;
1292 $to['d'] = date('t', mktime(0, 0, 0, $to['M'], 1, $to['Y']));
1298 switch ($relativeTerm) {
1301 $to['d'] = date('t', mktime(0, 0, 0, $now['mon'], 1, $now['year']));
1302 $from['M'] = $to['M'] = $now['mon'];
1303 $from['Y'] = $to['Y'] = $now['year'];
1308 if ($now['mon'] == 1) {
1309 $from['M'] = $to['M'] = 12;
1310 $from['Y'] = $to['Y'] = $now['year'] - 1;
1313 $from['M'] = $to['M'] = $now['mon'] - 1;
1314 $from['Y'] = $to['Y'] = $now['year'];
1316 $to['d'] = date('t', mktime(0, 0, 0, $to['M'], 1, $to['Y']));
1319 case 'previous_before':
1321 if ($now['mon'] < 3) {
1322 $from['M'] = $to['M'] = 10 +
$now['mon'];
1323 $from['Y'] = $to['Y'] = $now['year'] - 1;
1326 $from['M'] = $to['M'] = $now['mon'] - 2;
1327 $from['Y'] = $to['Y'] = $now['year'];
1329 $to['d'] = date('t', mktime(0, 0, 0, $to['M'], 1, $to['Y']));
1334 if ($now['mon'] < 3) {
1335 $from['M'] = 10 +
$now['mon'];
1336 $from['Y'] = $now['year'] - 1;
1339 $from['M'] = $now['mon'] - 2;
1340 $from['Y'] = $now['year'];
1343 if ($now['mon'] == 1) {
1345 $to['Y'] = $now['year'] - 1;
1348 $to['M'] = $now['mon'] - 1;
1349 $to['Y'] = $now['year'];
1352 $to['d'] = date('t', mktime(0, 0, 0, $to['M'], 1, $to['Y']));
1356 //before end of past month
1357 if ($now['mon'] == 1) {
1359 $to['Y'] = $now['year'] - 1;
1362 $to['M'] = $now['mon'] - 1;
1363 $to['Y'] = $now['year'];
1366 $to['d'] = date('t', mktime(0, 0, 0, $to['M'], 1, $to['Y']));
1372 $from['M'] = $now['mon'];;
1373 $from['Y'] = $now['year'];
1377 case 'greater_previous':
1378 //from end of past month
1379 if ($now['mon'] == 1) {
1381 $from['Y'] = $now['year'] - 1;
1384 $from['M'] = $now['mon'] - 1;
1385 $from['Y'] = $now['year'];
1388 $from['d'] = date('t', mktime(0, 0, 0, $from['M'], 1, $from['Y']));
1393 $to['d'] = $now['mday'];
1394 $to['M'] = $now['mon'];
1395 $to['Y'] = $now['year'];
1397 $to['i'] = $to['s'] = 59;
1398 $from = self
::intervalAdd('month', -1, $to);
1399 $from = self
::intervalAdd('second', 1, $from);
1404 $from['M'] = $now['mon'];;
1405 $from['Y'] = $now['year'];
1406 $to['d'] = $now['mday'];
1407 $to['M'] = $now['mon'];
1408 $to['Y'] = $now['year'];
1410 $to['i'] = $to['s'] = 59;
1415 $to['Y'] = $now['year'];
1416 $to['M'] = $now['mon'];
1417 $to['d'] = date('t', mktime(0, 0, 0, $now['mon'], 1, $now['year']));
1423 if ($now['mon'] == 12) {
1424 $from['M'] = $to['M'] = 1;
1425 $from['Y'] = $to['Y'] = $now['year'] +
1;
1428 $from['M'] = $to['M'] = $now['mon'] +
1;
1429 $from['Y'] = $to['Y'] = $now['year'];
1431 $to['d'] = date('t', mktime(0, 0, 0, $to['M'], 1, $to['Y']));
1435 $from['d'] = $now['mday'];
1436 $from['M'] = $now['mon'];
1437 $from['Y'] = $now['year'];
1439 $from['i'] = $to['s'] = 00;
1440 $to = self
::intervalAdd('month', 1, $from);
1441 $to = self
::intervalAdd('second', -1, $to);
1447 switch ($relativeTerm) {
1449 $from['d'] = $now['mday'];
1450 $from['M'] = $now['mon'];
1451 $from['Y'] = $now['year'];
1452 $from = self
::intervalAdd('day', -1 * ($now['wday']), $from);
1453 $to = self
::intervalAdd('day', 6, $from);
1457 $from['d'] = $now['mday'];
1458 $from['M'] = $now['mon'];
1459 $from['Y'] = $now['year'];
1460 $from = self
::intervalAdd('day', -1 * ($now['wday']) - 7, $from);
1461 $to = self
::intervalAdd('day', 6, $from);
1464 case 'previous_before':
1465 $from['d'] = $now['mday'];
1466 $from['M'] = $now['mon'];
1467 $from['Y'] = $now['year'];
1468 $from = self
::intervalAdd('day', -1 * ($now['wday']) - 14, $from);
1469 $to = self
::intervalAdd('day', 6, $from);
1473 $from['d'] = $now['mday'];
1474 $from['M'] = $now['mon'];
1475 $from['Y'] = $now['year'];
1476 $from = self
::intervalAdd('day', -1 * ($now['wday']) - 14, $from);
1477 $to = self
::intervalAdd('day', 13, $from);
1481 $to['d'] = $now['mday'];
1482 $to['M'] = $now['mon'];
1483 $to['Y'] = $now['year'];
1484 $to = self
::intervalAdd('day', -1 * ($now['wday']) - 1, $to);
1489 $from['d'] = $now['mday'];
1490 $from['M'] = $now['mon'];
1491 $from['Y'] = $now['year'];
1492 $from = self
::intervalAdd('day', -1 * ($now['wday']), $from);
1496 case 'greater_previous':
1497 $from['d'] = $now['mday'];
1498 $from['M'] = $now['mon'];
1499 $from['Y'] = $now['year'];
1500 $from = self
::intervalAdd('day', -1 * ($now['wday']) - 1, $from);
1505 $to['d'] = $now['mday'];
1506 $to['M'] = $now['mon'];
1507 $to['Y'] = $now['year'];
1509 $to['i'] = $to['s'] = 59;
1510 $from = self
::intervalAdd('day', -7, $to);
1511 $from = self
::intervalAdd('second', 1, $from);
1515 $from['d'] = $now['mday'];
1516 $from['M'] = $now['mon'];
1517 $from['Y'] = $now['year'];
1518 $from = self
::intervalAdd('day', -1 * ($now['wday']), $from);
1519 $to['d'] = $now['mday'];
1520 $to['M'] = $now['mon'];
1521 $to['Y'] = $now['year'];
1523 $to['i'] = $to['s'] = 59;
1527 $to['d'] = $now['mday'];
1528 $to['M'] = $now['mon'];
1529 $to['Y'] = $now['year'];
1531 $to = self
::intervalAdd('day', -1 * ($now['wday']) +
6, $to);
1536 $from['d'] = $now['mday'];
1537 $from['M'] = $now['mon'];
1538 $from['Y'] = $now['year'];
1539 $from = self
::intervalAdd('day', -1 * ($now['wday']) +
7, $from);
1540 $to = self
::intervalAdd('day', 6, $from);
1544 $from['d'] = $now['mday'];
1545 $from['M'] = $now['mon'];
1546 $from['Y'] = $now['year'];
1548 $from['i'] = $to['s'] = 00;
1549 $to = self
::intervalAdd('day', 7, $from);
1550 $to = self
::intervalAdd('second', -1, $to);
1556 switch ($relativeTerm) {
1558 $from['d'] = $to['d'] = $now['mday'];
1559 $from['M'] = $to['M'] = $now['mon'];
1560 $from['Y'] = $to['Y'] = $now['year'];
1564 $from['d'] = $now['mday'];
1565 $from['M'] = $now['mon'];
1566 $from['Y'] = $now['year'];
1567 $from = self
::intervalAdd('day', -1, $from);
1568 $to['d'] = $from['d'];
1569 $to['M'] = $from['M'];
1570 $to['Y'] = $from['Y'];
1573 case 'previous_before':
1574 $from['d'] = $now['mday'];
1575 $from['M'] = $now['mon'];
1576 $from['Y'] = $now['year'];
1577 $from = self
::intervalAdd('day', -2, $from);
1578 $to['d'] = $from['d'];
1579 $to['M'] = $from['M'];
1580 $to['Y'] = $from['Y'];
1584 $from['d'] = $to['d'] = $now['mday'];
1585 $from['M'] = $to['M'] = $now['mon'];
1586 $from['Y'] = $to['Y'] = $now['year'];
1587 $from = self
::intervalAdd('day', -2, $from);
1588 $to = self
::intervalAdd('day', -1, $to);
1592 $to['d'] = $now['mday'];
1593 $to['M'] = $now['mon'];
1594 $to['Y'] = $now['year'];
1599 $from['d'] = $now['mday'];
1600 $from['M'] = $now['mon'];;
1601 $from['Y'] = $now['year'];
1606 $to['d'] = $now['mday'];
1607 $to['M'] = $now['mon'];
1608 $to['Y'] = $now['year'];
1609 $to = self
::intervalAdd('day', 1, $to);
1610 $from['d'] = $to['d'];
1611 $from['M'] = $to['M'];
1612 $from['Y'] = $to['Y'];
1623 if (!empty($
$item)) {
1624 $dateRange[$item] = self
::format($
$item);
1627 $dateRange[$item] = NULL;
1634 * Calculate current fiscal year based on the fiscal month and day.
1636 * @param int $fyDate
1637 * Fiscal start date.
1639 * @param int $fyMonth
1640 * Fiscal Start Month.
1643 * $fy Current Fiscl Year
1645 public static function calculateFiscalYear($fyDate, $fyMonth) {
1646 $date = date("Y-m-d");
1647 $currentYear = date("Y");
1649 //recalculate the date because month 4::04 make the difference
1650 $fiscalYear = explode('-', date("Y-m-d", mktime(0, 0, 0, $fyMonth, $fyDate, $currentYear)));
1651 $fyDate = $fiscalYear[2];
1652 $fyMonth = $fiscalYear[1];
1653 $fyStartDate = date("Y-m-d", mktime(0, 0, 0, $fyMonth, $fyDate, $currentYear));
1655 if ($fyStartDate > $date) {
1656 $fy = intval(intval($currentYear) - 1);
1659 $fy = intval($currentYear);
1665 * Function to process date, convert to mysql format
1667 * @param string $date
1669 * @param string $time
1671 * @param bool|string $returnNullString 'null' needs to be returned
1672 * so that db oject will set null in db
1673 * @param string $format
1674 * Expected return date format.( default is mysql ).
1677 * date format that is excepted by mysql
1679 public static function processDate($date, $time = NULL, $returnNullString = FALSE, $format = 'YmdHis') {
1682 if ($returnNullString) {
1683 $mysqlDate = 'null';
1687 $mysqlDate = date($format, strtotime($date . ' ' . $time));
1694 * Function to convert mysql to date plugin format.
1696 * @param string $mysqlDate
1699 * @param null $formatType
1700 * @param null $format
1701 * @param null $timeFormat
1706 public static function setDateDefaults($mysqlDate = NULL, $formatType = NULL, $format = NULL, $timeFormat = NULL) {
1707 // if date is not passed assume it as today
1709 $mysqlDate = date('Y-m-d G:i:s');
1712 $config = CRM_Core_Config
::singleton();
1714 // get actual format
1715 $params = array('name' => $formatType);
1717 CRM_Core_DAO
::commonRetrieve('CRM_Core_DAO_PreferencesDate', $params, $values);
1719 if ($values['date_format']) {
1720 $format = $values['date_format'];
1723 if (isset($values['time_format'])) {
1724 $timeFormat = $values['time_format'];
1728 // now we set display date using js, hence we should always setdefault
1729 // 'm/d/Y' format. So that submitted value is alwats mm/dd/YY format
1730 // note that for date display we dynamically create text field
1733 $format = $config->dateInputFormat;
1736 // get actual format
1737 $actualPHPFormats = CRM_Core_SelectValues::datePluginToPHPFormats( );
1738 $dateFormat = CRM_Utils_Array::value( $format, $actualPHPFormats );
1741 $dateFormat = 'm/d/Y';
1742 $date = date($dateFormat, strtotime($mysqlDate));
1745 $timeFormat = $config->timeInputFormat
;
1748 $actualTimeFormat = "g:iA";
1749 $appendZeroLength = 7;
1750 if ($timeFormat > 1) {
1751 $actualTimeFormat = "G:i";
1752 $appendZeroLength = 5;
1755 $time = date($actualTimeFormat, strtotime($mysqlDate));
1757 // need to append zero for hours < 10
1758 if (strlen($time) < $appendZeroLength) {
1759 $time = '0' . $time;
1762 return array($date, $time);
1766 * Function get date format.
1768 * @param string $formatType
1769 * Date name e.g. birth.
1773 public static function getDateFormat($formatType = NULL) {
1776 $format = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_PreferencesDate',
1777 $formatType, 'date_format', 'name'
1782 $config = CRM_Core_Config
::singleton();
1783 $format = $config->dateInputFormat
;
1789 * Get the time in UTC for the current time. You can optionally send an offset from the current time if needed
1791 * @param int $offset
1792 * the offset from the current time in seconds.
1797 public static function getUTCTime($offset = 0) {
1798 $originalTimezone = date_default_timezone_get();
1799 date_default_timezone_set('UTC');
1800 $time = time() +
$offset;
1801 $now = date('YmdHis', $time);
1802 date_default_timezone_set($originalTimezone);
1811 * @return null|string
1813 public static function formatDate($date, $dateType) {
1814 $formattedDate = NULL;
1816 return $formattedDate;
1819 //1. first convert date to default format.
1820 //2. append time to default formatted date (might be removed during format)
1821 //3. validate date / date time.
1822 //4. If date and time then convert to default date time format.
1825 $dateParams = array($dateKey => $date);
1827 if (CRM_Utils_Date
::convertToDefaultDate($dateParams, $dateType, $dateKey)) {
1828 $dateVal = $dateParams[$dateKey];
1830 if ($dateType == 1) {
1832 if (preg_match("/(\s(([01]\d)|[2][0-3]):([0-5]\d))$/", $date, $matches)) {
1833 $ruleName = 'dateTime';
1834 if (strpos($date, '-') !== FALSE) {
1835 $dateVal .= array_shift($matches);
1841 $valid = CRM_Utils_Rule
::$ruleName($dateVal);
1844 //format date and time to default.
1845 if ($ruleName == 'dateTime') {
1846 $dateVal = CRM_Utils_Date
::customFormat(preg_replace("/(:|\s)?/", "", $dateVal), '%Y%m%d%H%i');
1847 //hack to add seconds
1850 $formattedDate = $dateVal;
1854 return $formattedDate;