3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
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-2018
37 class CRM_Utils_Date
{
40 * Format a date by padding it with leading '0'.
44 * @param string $separator
45 * The seperator to use when formatting the date.
46 * @param int|string $invalidDate what to return if the date is invalid
49 * formatted string for date
51 public static function format($date, $separator = '', $invalidDate = 0) {
52 if (is_numeric($date) &&
53 ((strlen($date) == 8) ||
(strlen($date) == 14))
58 if (!is_array($date) ||
59 CRM_Utils_System
::isNull($date) ||
65 $date['Y'] = (int ) $date['Y'];
66 if ($date['Y'] < 1000 ||
$date['Y'] > 2999) {
70 if (array_key_exists('m', $date)) {
71 $date['M'] = $date['m'];
73 elseif (array_key_exists('F', $date)) {
74 $date['M'] = $date['F'];
77 if (!empty($date['M'])) {
78 $date['M'] = (int ) $date['M'];
79 if ($date['M'] < 1 ||
$date['M'] > 12) {
87 if (!empty($date['d'])) {
88 $date['d'] = (int ) $date['d'];
94 if (!checkdate($date['M'], $date['d'], $date['Y'])) {
98 $date['M'] = sprintf('%02d', $date['M']);
99 $date['d'] = sprintf('%02d', $date['d']);
102 if (CRM_Utils_Array
::value('H', $date) != NULL ||
103 CRM_Utils_Array
::value('h', $date) != NULL ||
104 CRM_Utils_Array
::value('i', $date) != NULL ||
105 CRM_Utils_Array
::value('s', $date) != NULL
107 // we have time too..
108 if (!empty($date['h'])) {
109 if (CRM_Utils_Array
::value('A', $date) == 'PM' or CRM_Utils_Array
::value('a', $date) == 'pm') {
110 if ($date['h'] != 12) {
111 $date['h'] = $date['h'] +
12;
114 if ((CRM_Utils_Array
::value('A', $date) == 'AM' or CRM_Utils_Array
::value('a', $date) == 'am') &&
115 CRM_Utils_Array
::value('h', $date) == 12
120 $date['h'] = (int ) $date['h'];
126 // in 24-hour format the hour is under the 'H' key
127 if (!empty($date['H'])) {
128 $date['H'] = (int) $date['H'];
134 if (!empty($date['i'])) {
135 $date['i'] = (int ) $date['i'];
141 if ($date['h'] == 0 && $date['H'] != 0) {
142 $date['h'] = $date['H'];
145 if (!empty($date['s'])) {
146 $date['s'] = (int ) $date['s'];
152 $date['h'] = sprintf('%02d', $date['h']);
153 $date['i'] = sprintf('%02d', $date['i']);
154 $date['s'] = sprintf('%02d', $date['s']);
159 $time .= $date['h'] . $separator . $date['i'] . $separator . $date['s'];
162 return $date['Y'] . $separator . $date['M'] . $separator . $date['d'] . $time;
166 * Return abbreviated weekday names according to the locale.
168 * Array will be in localized order according to 'weekBegins' setting,
169 * but array keys will always match to:
175 * 0-based array with abbreviated weekday names
178 public static function getAbbrWeekdayNames() {
179 static $days = array();
181 // First day of the week
182 $firstDay = Civi
::settings()->get('weekBegins');
184 // set LC_TIME and build the arrays from locale-provided names
185 // June 1st, 1970 was a Monday
186 CRM_Core_I18n
::setLcTime();
187 for ($i = $firstDay; count($days) < 7; $i = $i > 5 ?
0 : $i +
1) {
188 $days[$i] = strftime('%a', mktime(0, 0, 0, 6, $i, 1970));
195 * Return full weekday names according to the locale.
197 * Array will be in localized order according to 'weekBegins' setting,
198 * but array keys will always match to:
204 * 0-based array with full weekday names
207 public static function getFullWeekdayNames() {
208 static $days = array();
210 // First day of the week
211 $firstDay = Civi
::settings()->get('weekBegins');
213 // set LC_TIME and build the arrays from locale-provided names
214 // June 1st, 1970 was a Monday
215 CRM_Core_I18n
::setLcTime();
216 for ($i = $firstDay; count($days) < 7; $i = $i > 5 ?
0 : $i +
1) {
217 $days[$i] = strftime('%A', mktime(0, 0, 0, 6, $i, 1970));
224 * Return abbreviated month names according to the locale.
229 * 1-based array with abbreviated month names
232 public static function &getAbbrMonthNames($month = FALSE) {
233 static $abbrMonthNames;
234 if (!isset($abbrMonthNames)) {
236 // set LC_TIME and build the arrays from locale-provided names
237 CRM_Core_I18n
::setLcTime();
238 for ($i = 1; $i <= 12; $i++
) {
239 $abbrMonthNames[$i] = strftime('%b', mktime(0, 0, 0, $i, 10, 1970));
243 return $abbrMonthNames[$month];
245 return $abbrMonthNames;
249 * Return full month names according to the locale.
252 * 1-based array with full month names
255 public static function &getFullMonthNames() {
256 static $fullMonthNames;
257 if (!isset($fullMonthNames)) {
259 // set LC_TIME and build the arrays from locale-provided names
260 CRM_Core_I18n
::setLcTime();
261 for ($i = 1; $i <= 12; $i++
) {
262 $fullMonthNames[$i] = strftime('%B', mktime(0, 0, 0, $i, 10, 1970));
265 return $fullMonthNames;
273 public static function unixTime($string) {
274 if (empty($string)) {
277 $parsedDate = date_parse($string);
278 return mktime(CRM_Utils_Array
::value('hour', $parsedDate),
279 CRM_Utils_Array
::value('minute', $parsedDate),
281 CRM_Utils_Array
::value('month', $parsedDate),
282 CRM_Utils_Array
::value('day', $parsedDate),
283 CRM_Utils_Array
::value('year', $parsedDate)
288 * Create a date and time string in a provided format.
290 * %b - abbreviated month name ('Jan'..'Dec')
291 * %B - full month name ('January'..'December')
292 * %d - day of the month as a decimal number, 0-padded ('01'..'31')
293 * %e - day of the month as a decimal number, blank-padded (' 1'..'31')
294 * %E - day of the month as a decimal number ('1'..'31')
295 * %f - English ordinal suffix for the day of the month ('st', 'nd', 'rd', 'th')
296 * %H - hour in 24-hour format, 0-padded ('00'..'23')
297 * %I - hour in 12-hour format, 0-padded ('01'..'12')
298 * %k - hour in 24-hour format, blank-padded (' 0'..'23')
299 * %l - hour in 12-hour format, blank-padded (' 1'..'12')
300 * %m - month as a decimal number, 0-padded ('01'..'12')
301 * %M - minute, 0-padded ('00'..'60')
302 * %p - lowercase ante/post meridiem ('am', 'pm')
303 * %P - uppercase ante/post meridiem ('AM', 'PM')
304 * %Y - year as a decimal number including the century ('2005')
306 * @param string $dateString
307 * Date and time in 'YYYY-MM-DD hh:mm:ss' format.
308 * @param string $format
310 * @param array $dateParts
311 * An array with the desired date parts.
314 * the $format-formatted $date
316 public static function customFormat($dateString, $format = NULL, $dateParts = NULL) {
317 // 1-based (January) month names arrays
318 $abbrMonths = self
::getAbbrMonthNames();
319 $fullMonths = self
::getFullMonthNames();
322 $config = CRM_Core_Config
::singleton();
325 if (array_intersect(array('h', 'H'), $dateParts)) {
326 $format = $config->dateformatDatetime
;
328 elseif (array_intersect(array('d', 'j'), $dateParts)) {
329 $format = $config->dateformatFull
;
331 elseif (array_intersect(array('m', 'M'), $dateParts)) {
332 $format = $config->dateformatPartial
;
335 $format = $config->dateformatYear
;
339 if (strpos($dateString, '-')) {
340 $month = (int) substr($dateString, 5, 2);
341 $day = (int) substr($dateString, 8, 2);
344 $month = (int) substr($dateString, 4, 2);
345 $day = (int) substr($dateString, 6, 2);
348 if (strlen($dateString) > 10) {
349 $format = $config->dateformatDatetime
;
352 $format = $config->dateformatFull
;
354 elseif ($month > 0) {
355 $format = $config->dateformatPartial
;
358 $format = $config->dateformatYear
;
363 if (!CRM_Utils_System
::isNull($dateString)) {
364 if (strpos($dateString, '-')) {
365 $year = (int) substr($dateString, 0, 4);
366 $month = (int) substr($dateString, 5, 2);
367 $day = (int) substr($dateString, 8, 2);
369 $hour24 = (int) substr($dateString, 11, 2);
370 $minute = (int) substr($dateString, 14, 2);
373 $year = (int) substr($dateString, 0, 4);
374 $month = (int) substr($dateString, 4, 2);
375 $day = (int) substr($dateString, 6, 2);
377 $hour24 = (int) substr($dateString, 8, 2);
378 $minute = (int) substr($dateString, 10, 2);
381 if ($day %
10 == 1 and $day != 11) {
384 elseif ($day %
10 == 2 and $day != 12) {
387 elseif ($day %
10 == 3 and $day != 13) {
408 $hour12 = $hour24 - 12;
414 '%b' => CRM_Utils_Array
::value($month, $abbrMonths),
415 '%B' => CRM_Utils_Array
::value($month, $fullMonths),
416 '%d' => $day > 9 ?
$day : '0' . $day,
417 '%e' => $day > 9 ?
$day : ' ' . $day,
420 '%H' => $hour24 > 9 ?
$hour24 : '0' . $hour24,
421 '%h' => $hour12 > 9 ?
$hour12 : '0' . $hour12,
422 '%I' => $hour12 > 9 ?
$hour12 : '0' . $hour12,
423 '%k' => $hour24 > 9 ?
$hour24 : ' ' . $hour24,
424 '%l' => $hour12 > 9 ?
$hour12 : ' ' . $hour12,
425 '%m' => $month > 9 ?
$month : '0' . $month,
426 '%M' => $minute > 9 ?
$minute : '0' . $minute,
427 '%i' => $minute > 9 ?
$minute : '0' . $minute,
428 '%p' => strtolower($type),
434 return strtr($format, $date);
442 * Converts the date/datetime from MySQL format to ISO format
444 * @param string $mysql
445 * Date/datetime in MySQL format.
448 * date/datetime in ISO format
450 public static function mysqlToIso($mysql) {
451 $year = substr($mysql, 0, 4);
452 $month = substr($mysql, 4, 2);
453 $day = substr($mysql, 6, 2);
454 $hour = substr($mysql, 8, 2);
455 $minute = substr($mysql, 10, 2);
456 $second = substr($mysql, 12, 2);
482 * Converts the date/datetime from ISO format to MySQL format
483 * Note that until CRM-14986/ 4.4.7 this was required whenever the pattern $dao->find(TRUE): $dao->save(); was
484 * used to update an object with a date field was used. The DAO now checks for a '-' in date field strings
485 * & runs this function if the - appears - meaning it is likely redundant in the form & BAO layers
488 * Date/datetime in ISO format.
491 * date/datetime in MySQL format
493 public static function isoToMysql($iso) {
494 $dropArray = array('-' => '', ':' => '', ' ' => '');
495 return strtr($iso, $dropArray);
499 * Converts the any given date to default date format.
501 * @param array $params
502 * Has given date-format.
503 * @param int $dateType
505 * @param string $dateParam
510 public static function convertToDefaultDate(&$params, $dateType, $dateParam) {
512 $cen = substr($now['year'], 0, 2);
516 if (!empty($params[$dateParam])) {
517 // suppress hh:mm or hh:mm:ss if it exists CRM-7957
518 $value = preg_replace("/(\s(([01]\d)|[2][0-3])(:([0-5]\d)){1,2})$/", "", $params[$dateParam]);
523 if (!preg_match('/^\d\d\d\d-?(\d|\d\d)-?(\d|\d\d)$/', $value)) {
529 if (!preg_match('/^(\d|\d\d)[-\/](\d|\d\d)[-\/]\d\d$/', $value)) {
535 if (!preg_match('/^(\d|\d\d)[-\/](\d|\d\d)[-\/]\d\d\d\d$/', $value)) {
541 if (!preg_match('/^[A-Za-z]*.[ \t]?\d\d\,[ \t]?\d\d\d\d$/', $value)) {
547 if (!preg_match('/^\d\d-[A-Za-z]{3}.*-\d\d$/', $value) && !preg_match('/^\d\d[-\/]\d\d[-\/]\d\d$/', $value)) {
553 if (!preg_match('/^(\d|\d\d)[-\/](\d|\d\d)[-\/]\d\d\d\d/', $value)) {
559 if ($dateType == 1) {
560 $formattedDate = explode("-", $value);
561 if (count($formattedDate) == 3) {
562 $year = (int) $formattedDate[0];
563 $month = (int) $formattedDate[1];
564 $day = (int) $formattedDate[2];
566 elseif (count($formattedDate) == 1 && (strlen($value) == 8)) {
574 if ($dateType == 2 ||
$dateType == 4) {
575 $formattedDate = explode("/", $value);
576 if (count($formattedDate) != 3) {
577 $formattedDate = explode("-", $value);
579 if (count($formattedDate) == 3) {
580 $year = (int) $formattedDate[2];
581 $month = (int) $formattedDate[0];
582 $day = (int) $formattedDate[1];
588 if ($dateType == 8) {
589 $dateArray = explode(' ', $value);
591 $dateArray[1] = (int) substr($dateArray[1], 0, 2);
594 $fullMonths = self
::getFullMonthNames();
595 foreach ($fullMonths as $key => $val) {
596 if (strtolower($dateArray[0]) == strtolower($val)) {
602 $abbrMonths = self
::getAbbrMonthNames();
603 foreach ($abbrMonths as $key => $val) {
604 if (strtolower(trim($dateArray[0], ".")) == strtolower($val)) {
610 $year = (int) $dateArray[2];
611 $day = (int) $dateArray[1];
612 $month = (int) $monthInt;
614 if ($dateType == 16) {
615 $dateArray = explode('-', $value);
616 if (count($dateArray) != 3) {
617 $dateArray = explode('/', $value);
620 if (count($dateArray) == 3) {
622 $fullMonths = self
::getFullMonthNames();
623 foreach ($fullMonths as $key => $val) {
624 if (strtolower($dateArray[1]) == strtolower($val)) {
630 $abbrMonths = self
::getAbbrMonthNames();
631 foreach ($abbrMonths as $key => $val) {
632 if (strtolower(trim($dateArray[1], ".")) == strtolower($val)) {
639 $monthInt = $dateArray[1];
642 $year = (int) $dateArray[2];
643 $day = (int) $dateArray[0];
644 $month = (int) $monthInt;
650 if ($dateType == 32) {
651 $formattedDate = explode("/", $value);
652 if (count($formattedDate) == 3) {
653 $year = (int) $formattedDate[2];
654 $month = (int) $formattedDate[1];
655 $day = (int) $formattedDate[0];
662 $month = ($month < 10) ?
"0" . "$month" : $month;
663 $day = ($day < 10) ?
"0" . "$day" : $day;
665 $year = (int ) $year;
666 // simple heuristic to determine what century to use
667 // 00 - 20 is always 2000 - 2020
668 // 21 - 99 is always 1921 - 1999
670 $year = (strlen($year) == 1) ?
$cen . '0' . $year : $cen . $year;
672 elseif ($year < 100) {
673 $year = $prevCen . $year;
676 if ($params[$dateParam]) {
677 $params[$dateParam] = "$year$month$day";
679 // if month is invalid return as error
680 if ($month !== '00' && $month <= 12) {
691 public static function isDate(&$date) {
692 if (CRM_Utils_System
::isNull($date)) {
699 * Translate a TTL to a concrete expiration time.
701 * @param NULL|int|DateInterval $ttl
702 * @param int $default
703 * The value to use if $ttl is not specified (NULL).
705 * Timestamp (seconds since epoch).
706 * @throws \CRM_Utils_Cache_InvalidArgumentException
708 public static function convertCacheTtlToExpires($ttl, $default) {
714 return time() +
$ttl;
716 elseif ($ttl instanceof DateInterval
) {
717 return date_add(new DateTime(), $ttl)->getTimestamp();
720 throw new CRM_Utils_Cache_InvalidArgumentException("Invalid cache TTL");
727 * @param NULL|int|DateInterval $ttl
728 * @param int $default
729 * The value to use if $ttl is not specified (NULL).
731 * Seconds until expiration.
732 * @throws \CRM_Utils_Cache_InvalidArgumentException
734 public static function convertCacheTtl($ttl, $default) {
738 elseif (is_int($ttl)) {
741 elseif ($ttl instanceof DateInterval
) {
742 return date_add(new DateTime(), $ttl)->getTimestamp() - time();
745 throw new CRM_Utils_Cache_InvalidArgumentException("Invalid cache TTL");
751 * @param null $timeStamp
753 * @return bool|string
755 public static function currentDBDate($timeStamp = NULL) {
756 return $timeStamp ?
date('YmdHis', $timeStamp) : date('YmdHis');
765 public static function overdue($date, $now = NULL) {
766 $mysqlDate = self
::isoToMysql($date);
768 $now = self
::currentDBDate();
771 $now = self
::isoToMysql($now);
774 return ($mysqlDate >= $now) ?
FALSE : TRUE;
778 * Get customized today.
780 * This function is used for getting customized today. To get
781 * actuall today pass 'dayParams' as null. or else pass the day,
782 * month, year values as array values
783 * Example: $dayParams = array(
784 * 'day' => '25', 'month' => '10',
785 * 'year' => '2007' );
787 * @param array $dayParams of the day, month, year.
788 * Array of the day, month, year.
790 * @param string $format
791 * Expected date format( default.
792 * format is 2007-12-21 )
795 * Return the customized today's date (Y-m-d)
797 public static function getToday($dayParams = NULL, $format = "Y-m-d") {
798 if (is_null($dayParams) ||
empty($dayParams)) {
799 $today = date($format);
802 $today = date($format, mktime(0, 0, 0,
813 * Find whether today's date lies in
816 * @param date $startDate
817 * Start date for the range.
818 * @param date $endDate
819 * End date for the range.
822 * true if today's date is in the given date range
824 public static function getRange($startDate, $endDate) {
825 $today = date("Y-m-d");
826 $mysqlStartDate = self
::isoToMysql($startDate);
827 $mysqlEndDate = self
::isoToMysql($endDate);
828 $mysqlToday = self
::isoToMysql($today);
830 if ((isset($mysqlStartDate) && isset($mysqlEndDate)) && (($mysqlToday >= $mysqlStartDate) && ($mysqlToday <= $mysqlEndDate))) {
833 elseif ((isset($mysqlStartDate) && !isset($mysqlEndDate)) && (($mysqlToday >= $mysqlStartDate))) {
836 elseif ((!isset($mysqlStartDate) && isset($mysqlEndDate)) && (($mysqlToday <= $mysqlEndDate))) {
843 * Get start date and end from
844 * the given relative term and unit
846 * @param string $relative Relative format in the format term.unit.
849 * @param string $from
851 * @param string $fromTime
852 * @param string $toTime
855 * start date, end date
857 public static function getFromTo($relative, $from, $to, $fromTime = NULL, $toTime = '235959') {
859 list($term, $unit) = explode('.', $relative, 2);
860 $dateRange = self
::relativeToAbsolute($term, $unit);
861 $from = substr($dateRange['from'], 0, 8);
862 $to = substr($dateRange['to'], 0, 8);
863 // @todo fix relativeToAbsolute & add tests
864 // relativeToAbsolute returns 8 char date strings
865 // or 14 char date + time strings.
866 // We should use those. However, it turns out to be unreliable.
867 // e.g. this.week does NOT return 235959 for 'from'
868 // so our defaults are more reliable.
869 // Currently relativeToAbsolute only supports 'whole' days so that is ok
872 $from = self
::processDate($from, $fromTime);
873 $to = self
::processDate($to, $toTime);
875 return array($from, $to);
879 * Calculate Age in Years if greater than one year else in months.
881 * @param date $birthDate
885 * array $results contains years or months
887 static public function calculateAge($birthDate) {
889 $formatedBirthDate = CRM_Utils_Date
::customFormat($birthDate, '%Y-%m-%d');
891 $bDate = explode('-', $formatedBirthDate);
892 $birthYear = $bDate[0];
893 $birthMonth = $bDate[1];
894 $birthDay = $bDate[2];
895 $year_diff = date("Y") - $birthYear;
897 // don't calculate age CRM-3143
898 if ($birthYear == '1902') {
902 switch ($year_diff) {
904 $month = (12 - $birthMonth) +
date("m");
906 if (date("d") < $birthDay) {
909 $results['months'] = $month;
911 elseif ($month == 12 && (date("d") < $birthDay)) {
912 $results['months'] = $month - 1;
915 $results['years'] = $year_diff;
920 $month = date("m") - $birthMonth;
921 $results['months'] = $month;
925 $results['years'] = $year_diff;
926 if ((date("m") < $birthMonth) ||
(date("m") == $birthMonth) && (date("d") < $birthDay)) {
935 * Calculate next payment date according to provided unit & interval
937 * @param string $unit
938 * Frequency unit like year,month, week etc.
940 * @param int $interval
941 * Frequency interval.
944 * Start date of pledge.
946 * @param bool $dontCareTime
949 * contains new date with added interval
951 public static function intervalAdd($unit, $interval, $date, $dontCareTime = FALSE) {
952 if (is_array($date)) {
953 $hour = CRM_Utils_Array
::value('H', $date);
954 $minute = CRM_Utils_Array
::value('i', $date);
955 $second = CRM_Utils_Array
::value('s', $date);
956 $month = CRM_Utils_Array
::value('M', $date);
957 $day = CRM_Utils_Array
::value('d', $date);
958 $year = CRM_Utils_Array
::value('Y', $date);
961 extract(date_parse($date));
963 $date = mktime($hour, $minute, $second, $month, $day, $year);
966 $date = mktime($hour, $minute, $second, $month, $day, $year +
$interval);
970 $date = mktime($hour, $minute, $second, $month +
$interval, $day, $year);
974 $interval = $interval * 7;
975 $date = mktime($hour, $minute, $second, $month, $day +
$interval, $year);
979 $date = mktime($hour, $minute, $second, $month, $day +
$interval, $year);
983 $date = mktime($hour, $minute, $second +
$interval, $month, $day, $year);
987 $scheduleDate = explode("-", date("n-j-Y-H-i-s", $date));
990 $date['M'] = $scheduleDate[0];
991 $date['d'] = $scheduleDate[1];
992 $date['Y'] = $scheduleDate[2];
993 if ($dontCareTime == FALSE) {
994 $date['H'] = $scheduleDate[3];
995 $date['i'] = $scheduleDate[4];
996 $date['s'] = $scheduleDate[5];
1002 * Get the smarty view presentation mapping for the given format.
1004 * Historically it was decided that where the view format is 'dd/mm/yy' or 'mm/dd/yy'
1005 * they should be rendered using a longer date format. This is likely as much to
1006 * do with the earlier date widget being unable to handle some formats as usablity.
1007 * However, we continue to respect this.
1010 * Given format ( eg 'M Y', 'Y M' ).
1012 * @return string|null
1013 * Smarty translation of the date format. Null is also valid and is translated
1014 * according to the available parts at the smarty layer.
1016 public static function getDateFieldViewFormat($format) {
1017 $supportableFormats = array(
1018 'mm/dd' => '%B %E%f',
1019 'dd-mm' => '%E%f %B',
1023 'dd/mm/yy' => '%E%f %B %Y',
1026 return array_key_exists($format, $supportableFormats) ?
$supportableFormats[$format] : self
::pickBestSmartyFormat($format);
1030 * Pick the smarty format from settings that best matches the time string we have.
1032 * For view purposes we historically use the setting that most closely matches the data
1033 * in the format from our settings, as opposed to the setting configured for the field.
1038 public static function pickBestSmartyFormat($format) {
1039 if (stristr($format, 'h')) {
1040 return Civi
::settings()->get('dateformatDatetime');
1042 if (stristr($format, 'd') ||
stristr($format, 'j')) {
1043 return Civi
::settings()->get('dateformatFull');
1045 if (stristr($format, 'm')) {
1046 return Civi
::settings()->get('dateformatPartial');
1048 return Civi
::settings()->get('dateformatYear');
1052 * Map date plugin and actual format that is used by PHP.
1056 public static function datePluginToPHPFormats() {
1057 $dateInputFormats = array(
1058 "mm/dd/yy" => 'm/d/Y',
1059 "dd/mm/yy" => 'd/m/Y',
1060 "yy-mm-dd" => 'Y-m-d',
1061 "dd-mm-yy" => 'd-m-Y',
1062 "dd.mm.yy" => 'd.m.Y',
1064 "M d, yy" => 'M j, Y',
1065 "d M yy" => 'j M Y',
1066 "MM d, yy" => 'F j, Y',
1067 "d MM yy" => 'j F Y',
1068 "DD, d MM yy" => 'l, j F Y',
1076 return $dateInputFormats;
1080 * Resolves the given relative time interval into finite time limits.
1082 * @param string $relativeTerm
1083 * Relative time frame: this, previous, previous_1.
1085 * Frequency unit like year, month, week etc.
1088 * start date and end date for the relative time frame
1090 public static function relativeToAbsolute($relativeTerm, $unit) {
1092 $from = $to = $dateRange = array();
1093 $from['H'] = $from['i'] = $from['s'] = 0;
1094 $relativeTermParts = explode('_', $relativeTerm);
1095 $relativeTermPrefix = $relativeTermParts[0];
1096 $relativeTermSuffix = isset($relativeTermParts[1]) ?
$relativeTermParts[1] : '';
1100 switch ($relativeTerm) {
1102 $from['d'] = $from['M'] = 1;
1105 $to['Y'] = $from['Y'] = $now['year'];
1109 $from['M'] = $from['d'] = 1;
1112 $to['Y'] = $from['Y'] = $now['year'] - 1;
1115 case 'previous_before':
1116 $from['M'] = $from['d'] = 1;
1119 $to['Y'] = $from['Y'] = $now['year'] - 2;
1123 $from['M'] = $from['d'] = 1;
1126 $from['Y'] = $now['year'] - 2;
1127 $to['Y'] = $now['year'] - 1;
1133 $to['Y'] = $now['year'] - 1;
1138 $from['M'] = $from['d'] = 1;
1139 $from['Y'] = $now['year'];
1143 case 'greater_previous':
1146 $from['Y'] = $now['year'] - 1;
1151 $to['d'] = $now['mday'];
1152 $to['M'] = $now['mon'];
1153 $to['Y'] = $now['year'];
1155 $to['i'] = $to['s'] = 59;
1156 $from = self
::intervalAdd('year', -1, $to);
1157 $from = self
::intervalAdd('second', 1, $from);
1161 $from['M'] = $from['d'] = 1;
1162 $from['Y'] = $now['year'];
1164 $to['i'] = $to['s'] = 59;
1165 $to['d'] = $now['mday'];
1166 $to['M'] = $now['mon'];
1167 $to['Y'] = $now['year'];
1171 $to['d'] = $now['mday'];
1172 $to['M'] = $now['mon'];
1173 $to['Y'] = $now['year'];
1175 $to['i'] = $to['s'] = 59;
1176 $from = self
::intervalAdd('year', -2, $to);
1177 $from = self
::intervalAdd('second', 1, $from);
1181 $to['d'] = $now['mday'];
1182 $to['M'] = $now['mon'];
1183 $to['Y'] = $now['year'];
1185 $to['i'] = $to['s'] = 59;
1186 $from = self
::intervalAdd('year', -3, $to);
1187 $from = self
::intervalAdd('second', 1, $from);
1193 $to['Y'] = $now['year'];
1198 $from['M'] = $from['d'] = 1;
1201 $to['Y'] = $from['Y'] = $now['year'] +
1;
1205 $from['d'] = $now['mday'];
1206 $from['M'] = $now['mon'];
1207 $from['Y'] = $now['year'];
1208 $to['d'] = $now['mday'] - 1;
1209 $to['M'] = $now['mon'];
1210 $to['Y'] = $now['year'] +
1;
1216 $config = CRM_Core_Config
::singleton();
1217 $from['d'] = $config->fiscalYearStart
['d'];
1218 $from['M'] = $config->fiscalYearStart
['M'];
1219 $fYear = self
::calculateFiscalYear($from['d'], $from['M']);
1220 switch ($relativeTermPrefix) {
1222 $from['Y'] = $fYear;
1223 $fiscalYear = mktime(0, 0, 0, $from['M'], $from['d'] - 1, $from['Y'] +
1);
1224 $fiscalEnd = explode('-', date("Y-m-d", $fiscalYear));
1226 $to['d'] = $fiscalEnd['2'];
1227 $to['M'] = $fiscalEnd['1'];
1228 $to['Y'] = $fiscalEnd['0'];
1232 if (!is_numeric($relativeTermSuffix)) {
1233 $from['Y'] = ($relativeTermSuffix === 'before') ?
$fYear - 2 : $fYear - 1;
1234 $fiscalYear = mktime(0, 0, 0, $from['M'], $from['d'] - 1, $from['Y'] +
1);
1235 $fiscalEnd = explode('-', date("Y-m-d", $fiscalYear));
1236 $to['d'] = $fiscalEnd['2'];
1237 $to['M'] = $fiscalEnd['1'];
1238 $to['Y'] = $fiscalEnd['0'];
1241 $from['Y'] = $fYear - $relativeTermSuffix;
1242 $fiscalYear = mktime(0, 0, 0, $from['M'], $from['d'] - 1, $from['Y'] +
1);
1243 $fiscalEnd = explode('-', date("Y-m-d", $fiscalYear));
1244 $to['d'] = $fiscalEnd['2'];
1245 $to['M'] = $fiscalEnd['1'];
1251 $from['Y'] = $fYear +
1;
1252 $fiscalYear = mktime(0, 0, 0, $from['M'], $from['d'] - 1, $from['Y'] +
1);
1253 $fiscalEnd = explode('-', date("Y-m-d", $fiscalYear));
1254 $to['d'] = $fiscalEnd['2'];
1255 $to['M'] = $fiscalEnd['1'];
1256 $to['Y'] = $fiscalEnd['0'];
1262 switch ($relativeTerm) {
1265 $quarter = ceil($now['mon'] / 3);
1267 $from['M'] = (3 * $quarter) - 2;
1268 $to['M'] = 3 * $quarter;
1269 $to['Y'] = $from['Y'] = $now['year'];
1270 $to['d'] = date('t', mktime(0, 0, 0, $to['M'], 1, $now['year']));
1275 $quarter = ceil($now['mon'] / 3);
1276 $quarter = $quarter - $difference;
1278 if ($quarter <= 0) {
1283 $from['M'] = (3 * $quarter) - 2;
1284 $to['M'] = 3 * $quarter;
1285 $to['Y'] = $from['Y'] = $now['year'] - $subtractYear;
1286 $to['d'] = date('t', mktime(0, 0, 0, $to['M'], 1, $to['Y']));
1289 case 'previous_before':
1291 $quarter = ceil($now['mon'] / 3);
1292 $quarter = $quarter - $difference;
1294 if ($quarter <= 0) {
1299 $from['M'] = (3 * $quarter) - 2;
1300 $to['M'] = 3 * $quarter;
1301 $to['Y'] = $from['Y'] = $now['year'] - $subtractYear;
1302 $to['d'] = date('t', mktime(0, 0, 0, $to['M'], 1, $to['Y']));
1307 $quarter = ceil($now['mon'] / 3);
1308 $current_quarter = $quarter;
1309 $quarter = $quarter - $difference;
1311 if ($quarter <= 0) {
1316 $from['M'] = (3 * $quarter) - 2;
1317 switch ($current_quarter) {
1319 $to['M'] = (4 * $quarter);
1323 $to['M'] = (4 * $quarter) +
3;
1327 $to['M'] = (4 * $quarter) +
2;
1331 $to['M'] = (4 * $quarter) +
1;
1334 $to['Y'] = $from['Y'] = $now['year'] - $subtractYear;
1335 if ($to['M'] > 12) {
1336 $to['M'] = 3 * ($quarter - 3);
1337 $to['Y'] = $now['year'];
1339 $to['d'] = date('t', mktime(0, 0, 0, $to['M'], 1, $to['Y']));
1343 $quarter = ceil($now['mon'] / 3) - 1;
1345 if ($quarter <= 0) {
1349 $to['M'] = 3 * $quarter;
1350 $to['Y'] = $from['Y'] = $now['year'] - $subtractYear;
1351 $to['d'] = date('t', mktime(0, 0, 0, $to['M'], 1, $to['Y']));
1356 $quarter = ceil($now['mon'] / 3);
1358 $from['M'] = (3 * $quarter) - 2;
1359 $from['Y'] = $now['year'];
1363 case 'greater_previous':
1364 $quarter = ceil($now['mon'] / 3) - 1;
1366 if ($quarter <= 0) {
1370 $from['M'] = 3 * $quarter;
1371 $from['Y'] = $from['Y'] = $now['year'] - $subtractYear;
1372 $from['d'] = date('t', mktime(0, 0, 0, $from['M'], 1, $from['Y']));
1377 $to['d'] = $now['mday'];
1378 $to['M'] = $now['mon'];
1379 $to['Y'] = $now['year'];
1381 $to['i'] = $to['s'] = 59;
1382 $from = self
::intervalAdd('day', -90, $to);
1383 $from = self
::intervalAdd('second', 1, $from);
1387 $quarter = ceil($now['mon'] / 3);
1389 $from['M'] = (3 * $quarter) - 2;
1390 $from['Y'] = $now['year'];
1391 $to['d'] = $now['mday'];
1392 $to['M'] = $now['mon'];
1393 $to['Y'] = $now['year'];
1395 $to['i'] = $to['s'] = 59;
1399 $quarter = ceil($now['mon'] / 3);
1400 $to['M'] = 3 * $quarter;
1401 $to['Y'] = $now['year'];
1402 $to['d'] = date('t', mktime(0, 0, 0, $to['M'], 1, $now['year']));
1409 $quarter = ceil($now['mon'] / 3);
1410 $quarter = $quarter - $difference;
1413 $now['year'] = $now['year'] +
1;
1416 if ($quarter <= 0) {
1421 $from['M'] = (3 * $quarter) - 2;
1422 $to['M'] = 3 * $quarter;
1423 $to['Y'] = $from['Y'] = $now['year'] - $subtractYear;
1424 $to['d'] = date('t', mktime(0, 0, 0, $to['M'], 1, $to['Y']));
1428 $from['d'] = $now['mday'];
1429 $from['M'] = $now['mon'];
1430 $from['Y'] = $now['year'];
1432 $from['i'] = $to['s'] = 00;
1433 $to = self
::intervalAdd('day', 90, $from);
1434 $to = self
::intervalAdd('second', -1, $to);
1440 switch ($relativeTerm) {
1443 $to['d'] = date('t', mktime(0, 0, 0, $now['mon'], 1, $now['year']));
1444 $from['M'] = $to['M'] = $now['mon'];
1445 $from['Y'] = $to['Y'] = $now['year'];
1450 if ($now['mon'] == 1) {
1451 $from['M'] = $to['M'] = 12;
1452 $from['Y'] = $to['Y'] = $now['year'] - 1;
1455 $from['M'] = $to['M'] = $now['mon'] - 1;
1456 $from['Y'] = $to['Y'] = $now['year'];
1458 $to['d'] = date('t', mktime(0, 0, 0, $to['M'], 1, $to['Y']));
1461 case 'previous_before':
1463 if ($now['mon'] < 3) {
1464 $from['M'] = $to['M'] = 10 +
$now['mon'];
1465 $from['Y'] = $to['Y'] = $now['year'] - 1;
1468 $from['M'] = $to['M'] = $now['mon'] - 2;
1469 $from['Y'] = $to['Y'] = $now['year'];
1471 $to['d'] = date('t', mktime(0, 0, 0, $to['M'], 1, $to['Y']));
1476 if ($now['mon'] < 3) {
1477 $from['M'] = 10 +
$now['mon'];
1478 $from['Y'] = $now['year'] - 1;
1481 $from['M'] = $now['mon'] - 2;
1482 $from['Y'] = $now['year'];
1485 if ($now['mon'] == 1) {
1487 $to['Y'] = $now['year'] - 1;
1490 $to['M'] = $now['mon'] - 1;
1491 $to['Y'] = $now['year'];
1494 $to['d'] = date('t', mktime(0, 0, 0, $to['M'], 1, $to['Y']));
1498 // before end of past month
1499 if ($now['mon'] == 1) {
1501 $to['Y'] = $now['year'] - 1;
1504 $to['M'] = $now['mon'] - 1;
1505 $to['Y'] = $now['year'];
1508 $to['d'] = date('t', mktime(0, 0, 0, $to['M'], 1, $to['Y']));
1514 $from['M'] = $now['mon'];;
1515 $from['Y'] = $now['year'];
1519 case 'greater_previous':
1520 // from end of past month
1521 if ($now['mon'] == 1) {
1523 $from['Y'] = $now['year'] - 1;
1526 $from['M'] = $now['mon'] - 1;
1527 $from['Y'] = $now['year'];
1530 $from['d'] = date('t', mktime(0, 0, 0, $from['M'], 1, $from['Y']));
1535 $to['d'] = $now['mday'];
1536 $to['M'] = $now['mon'];
1537 $to['Y'] = $now['year'];
1539 $to['i'] = $to['s'] = 59;
1540 $from = self
::intervalAdd('day', -60, $to);
1541 $from = self
::intervalAdd('second', 1, $from);
1545 $to['d'] = $now['mday'];
1546 $to['M'] = $now['mon'];
1547 $to['Y'] = $now['year'];
1549 $to['i'] = $to['s'] = 59;
1550 $from = self
::intervalAdd('day', -30, $to);
1551 $from = self
::intervalAdd('second', 1, $from);
1556 $from['M'] = $now['mon'];;
1557 $from['Y'] = $now['year'];
1558 $to['d'] = $now['mday'];
1559 $to['M'] = $now['mon'];
1560 $to['Y'] = $now['year'];
1562 $to['i'] = $to['s'] = 59;
1567 $to['Y'] = $now['year'];
1568 $to['M'] = $now['mon'];
1569 $to['d'] = date('t', mktime(0, 0, 0, $now['mon'], 1, $now['year']));
1575 if ($now['mon'] == 12) {
1576 $from['M'] = $to['M'] = 1;
1577 $from['Y'] = $to['Y'] = $now['year'] +
1;
1580 $from['M'] = $to['M'] = $now['mon'] +
1;
1581 $from['Y'] = $to['Y'] = $now['year'];
1583 $to['d'] = date('t', mktime(0, 0, 0, $to['M'], 1, $to['Y']));
1587 $from['d'] = $now['mday'];
1588 $from['M'] = $now['mon'];
1589 $from['Y'] = $now['year'];
1591 $from['i'] = $to['s'] = 00;
1592 $to = self
::intervalAdd('day', 30, $from);
1593 $to = self
::intervalAdd('second', -1, $to);
1597 $from['d'] = $now['mday'];
1598 $from['M'] = $now['mon'];
1599 $from['Y'] = $now['year'];
1601 $from['i'] = $to['s'] = 00;
1602 $to = self
::intervalAdd('day', 60, $from);
1603 $to = self
::intervalAdd('second', -1, $to);
1609 $weekFirst = Civi
::settings()->get('weekBegins');
1610 $thisDay = $now['wday'];
1611 if ($weekFirst > $thisDay) {
1612 $diffDay = $thisDay - $weekFirst +
7;
1615 $diffDay = $thisDay - $weekFirst;
1617 switch ($relativeTerm) {
1619 $from['d'] = $now['mday'];
1620 $from['M'] = $now['mon'];
1621 $from['Y'] = $now['year'];
1622 $from = self
::intervalAdd('day', -1 * ($diffDay), $from);
1623 $to = self
::intervalAdd('day', 6, $from);
1627 $from['d'] = $now['mday'];
1628 $from['M'] = $now['mon'];
1629 $from['Y'] = $now['year'];
1630 $from = self
::intervalAdd('day', -1 * ($diffDay) - 7, $from);
1631 $to = self
::intervalAdd('day', 6, $from);
1634 case 'previous_before':
1635 $from['d'] = $now['mday'];
1636 $from['M'] = $now['mon'];
1637 $from['Y'] = $now['year'];
1638 $from = self
::intervalAdd('day', -1 * ($diffDay) - 14, $from);
1639 $to = self
::intervalAdd('day', 6, $from);
1643 $from['d'] = $now['mday'];
1644 $from['M'] = $now['mon'];
1645 $from['Y'] = $now['year'];
1646 $from = self
::intervalAdd('day', -1 * ($diffDay) - 14, $from);
1647 $to = self
::intervalAdd('day', 13, $from);
1651 $to['d'] = $now['mday'];
1652 $to['M'] = $now['mon'];
1653 $to['Y'] = $now['year'];
1654 $to = self
::intervalAdd('day', -1 * ($diffDay) - 1, $to);
1659 $from['d'] = $now['mday'];
1660 $from['M'] = $now['mon'];
1661 $from['Y'] = $now['year'];
1662 $from = self
::intervalAdd('day', -1 * ($diffDay), $from);
1666 case 'greater_previous':
1667 $from['d'] = $now['mday'];
1668 $from['M'] = $now['mon'];
1669 $from['Y'] = $now['year'];
1670 $from = self
::intervalAdd('day', -1 * ($diffDay) - 1, $from);
1675 $to['d'] = $now['mday'];
1676 $to['M'] = $now['mon'];
1677 $to['Y'] = $now['year'];
1679 $to['i'] = $to['s'] = 59;
1680 $from = self
::intervalAdd('day', -7, $to);
1681 $from = self
::intervalAdd('second', 1, $from);
1685 $from['d'] = $now['mday'];
1686 $from['M'] = $now['mon'];
1687 $from['Y'] = $now['year'];
1688 $from = self
::intervalAdd('day', -1 * ($diffDay), $from);
1689 $to['d'] = $now['mday'];
1690 $to['M'] = $now['mon'];
1691 $to['Y'] = $now['year'];
1693 $to['i'] = $to['s'] = 59;
1697 $to['d'] = $now['mday'];
1698 $to['M'] = $now['mon'];
1699 $to['Y'] = $now['year'];
1701 $to = self
::intervalAdd('day', -1 * ($diffDay) +
6, $to);
1706 $from['d'] = $now['mday'];
1707 $from['M'] = $now['mon'];
1708 $from['Y'] = $now['year'];
1709 $from = self
::intervalAdd('day', -1 * ($diffDay) +
7, $from);
1710 $to = self
::intervalAdd('day', 6, $from);
1714 $from['d'] = $now['mday'];
1715 $from['M'] = $now['mon'];
1716 $from['Y'] = $now['year'];
1718 $from['i'] = $to['s'] = 00;
1719 $to = self
::intervalAdd('day', 7, $from);
1720 $to = self
::intervalAdd('second', -1, $to);
1726 switch ($relativeTerm) {
1728 $from['d'] = $to['d'] = $now['mday'];
1729 $from['M'] = $to['M'] = $now['mon'];
1730 $from['Y'] = $to['Y'] = $now['year'];
1734 $from['d'] = $now['mday'];
1735 $from['M'] = $now['mon'];
1736 $from['Y'] = $now['year'];
1737 $from = self
::intervalAdd('day', -1, $from);
1738 $to['d'] = $from['d'];
1739 $to['M'] = $from['M'];
1740 $to['Y'] = $from['Y'];
1743 case 'previous_before':
1744 $from['d'] = $now['mday'];
1745 $from['M'] = $now['mon'];
1746 $from['Y'] = $now['year'];
1747 $from = self
::intervalAdd('day', -2, $from);
1748 $to['d'] = $from['d'];
1749 $to['M'] = $from['M'];
1750 $to['Y'] = $from['Y'];
1754 $from['d'] = $to['d'] = $now['mday'];
1755 $from['M'] = $to['M'] = $now['mon'];
1756 $from['Y'] = $to['Y'] = $now['year'];
1757 $from = self
::intervalAdd('day', -2, $from);
1758 $to = self
::intervalAdd('day', -1, $to);
1762 $to['d'] = $now['mday'];
1763 $to['M'] = $now['mon'];
1764 $to['Y'] = $now['year'];
1769 $from['d'] = $now['mday'];
1770 $from['M'] = $now['mon'];;
1771 $from['Y'] = $now['year'];
1776 $to['d'] = $now['mday'];
1777 $to['M'] = $now['mon'];
1778 $to['Y'] = $now['year'];
1779 $to = self
::intervalAdd('day', 1, $to);
1780 $from['d'] = $to['d'];
1781 $from['M'] = $to['M'];
1782 $from['Y'] = $to['Y'];
1793 if (!empty($
$item)) {
1794 $dateRange[$item] = self
::format($
$item);
1797 $dateRange[$item] = NULL;
1804 * Calculate current fiscal year based on the fiscal month and day.
1806 * @param int $fyDate
1807 * Fiscal start date.
1809 * @param int $fyMonth
1810 * Fiscal Start Month.
1813 * $fy Current Fiscal Year
1815 public static function calculateFiscalYear($fyDate, $fyMonth) {
1816 $date = date("Y-m-d");
1817 $currentYear = date("Y");
1819 // recalculate the date because month 4::04 make the difference
1820 $fiscalYear = explode('-', date("Y-m-d", mktime(0, 0, 0, $fyMonth, $fyDate, $currentYear)));
1821 $fyDate = $fiscalYear[2];
1822 $fyMonth = $fiscalYear[1];
1823 $fyStartDate = date("Y-m-d", mktime(0, 0, 0, $fyMonth, $fyDate, $currentYear));
1825 if ($fyStartDate > $date) {
1826 $fy = intval(intval($currentYear) - 1);
1829 $fy = intval($currentYear);
1835 * Function to process date, convert to mysql format
1837 * @param string $date
1839 * @param string $time
1841 * @param bool|string $returnNullString 'null' needs to be returned
1842 * so that db oject will set null in db
1843 * @param string $format
1844 * Expected return date format.( default is mysql ).
1847 * date format that is excepted by mysql
1849 public static function processDate($date, $time = NULL, $returnNullString = FALSE, $format = 'YmdHis') {
1852 if ($returnNullString) {
1853 $mysqlDate = 'null';
1857 $mysqlDate = date($format, strtotime($date . ' ' . $time));
1864 * Add the metadata about a date field to the field.
1866 * This metadata will work with the call $form->add('datepicker', ...
1868 * @param array $fieldMetaData
1869 * @param array $field
1873 public static function addDateMetadataToField($fieldMetaData, $field) {
1874 if (isset($fieldMetaData['html'])) {
1875 $field['html_type'] = $fieldMetaData['html']['type'];
1876 if ($field['html_type'] === 'Select Date') {
1877 if (!isset($field['date_format'])) {
1878 $dateAttributes = CRM_Core_SelectValues
::date($fieldMetaData['html']['formatType'], NULL, NULL, NULL, 'Input');
1879 $field['start_date_years'] = $dateAttributes['minYear'];
1880 $field['end_date_years'] = $dateAttributes['maxYear'];
1881 $field['date_format'] = $dateAttributes['format'];
1882 $field['is_datetime_field'] = TRUE;
1883 $field['time_format'] = $dateAttributes['time'];
1884 $field['smarty_view_format'] = $dateAttributes['smarty_view_format'];
1886 $field['datepicker']['extra'] = self
::getDatePickerExtra($field);
1887 $field['datepicker']['attributes'] = self
::getDatePickerAttributes($field);
1895 * Get the fields required for the 'extra' parameter when adding a datepicker.
1897 * @param array $field
1901 public static function getDatePickerExtra($field) {
1903 if (isset($field['date_format'])) {
1904 $extra['date'] = $field['date_format'];
1905 $extra['time'] = $field['time_format'];
1907 $thisYear = date('Y');
1908 if (isset($field['start_date_years'])) {
1909 $extra['minDate'] = date('Y-m-d', strtotime('-' . ($thisYear - $field['start_date_years']) . ' years'));
1911 if (isset($field['end_date_years'])) {
1912 $extra['maxDate'] = date('Y-m-d', strtotime('-' . ($thisYear - $field['end_date_years']) . ' years'));
1918 * Get the attributes parameters required for datepicker.
1920 * @param array $field
1924 * Array ready to pass to $this->addForm('datepicker' as attributes.
1926 public static function getDatePickerAttributes(&$field) {
1927 $attributes = array();
1928 $dateAttributes = array(
1929 'start_date_years' => 'minYear',
1930 'end_date_years' => 'maxYear',
1931 'date_format' => 'format',
1933 foreach ($dateAttributes as $dateAttribute => $mapTo) {
1934 if (isset($field[$dateAttribute])) {
1935 $attributes[$mapTo] = $field[$dateAttribute];
1942 * Function to convert mysql to date plugin format.
1944 * @param string $mysqlDate
1947 * @param null $formatType
1948 * @param null $format
1949 * @param null $timeFormat
1954 public static function setDateDefaults($mysqlDate = NULL, $formatType = NULL, $format = NULL, $timeFormat = NULL) {
1955 // if date is not passed assume it as today
1957 $mysqlDate = date('Y-m-d G:i:s');
1960 $config = CRM_Core_Config
::singleton();
1962 // get actual format
1963 $params = array('name' => $formatType);
1965 CRM_Core_DAO
::commonRetrieve('CRM_Core_DAO_PreferencesDate', $params, $values);
1967 if ($values['date_format']) {
1968 $format = $values['date_format'];
1971 if (isset($values['time_format'])) {
1972 $timeFormat = $values['time_format'];
1976 // now we set display date using js, hence we should always setdefault
1977 // 'm/d/Y' format. So that submitted value is alwats mm/dd/YY format
1978 // note that for date display we dynamically create text field
1981 $format = $config->dateInputFormat;
1984 // get actual format
1985 $actualPHPFormats = CRM_Core_SelectValues::datePluginToPHPFormats( );
1986 $dateFormat = CRM_Utils_Array::value( $format, $actualPHPFormats );
1989 $dateFormat = 'm/d/Y';
1990 $date = date($dateFormat, strtotime($mysqlDate));
1993 $timeFormat = $config->timeInputFormat
;
1996 $actualTimeFormat = "g:iA";
1997 $appendZeroLength = 7;
1998 if ($timeFormat > 1) {
1999 $actualTimeFormat = "G:i";
2000 $appendZeroLength = 5;
2003 $time = date($actualTimeFormat, strtotime($mysqlDate));
2005 // need to append zero for hours < 10
2006 if (strlen($time) < $appendZeroLength) {
2007 $time = '0' . $time;
2010 return array($date, $time);
2014 * Function get date format.
2016 * @param string $formatType
2017 * Date name e.g. birth.
2021 public static function getDateFormat($formatType = NULL) {
2024 $format = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_PreferencesDate',
2025 $formatType, 'date_format', 'name'
2030 $config = CRM_Core_Config
::singleton();
2031 $format = $config->dateInputFormat
;
2040 * @return null|string
2042 public static function formatDate($date, $dateType) {
2043 $formattedDate = NULL;
2045 return $formattedDate;
2048 // 1. first convert date to default format.
2049 // 2. append time to default formatted date (might be removed during format)
2050 // 3. validate date / date time.
2051 // 4. If date and time then convert to default date time format.
2054 $dateParams = array($dateKey => $date);
2056 if (CRM_Utils_Date
::convertToDefaultDate($dateParams, $dateType, $dateKey)) {
2057 $dateVal = $dateParams[$dateKey];
2059 if ($dateType == 1) {
2061 if (preg_match("/(\s(([01]\d)|[2][0-3]):([0-5]\d))$/", $date, $matches)) {
2062 $ruleName = 'dateTime';
2063 if (strpos($date, '-') !== FALSE) {
2064 $dateVal .= array_shift($matches);
2070 $valid = CRM_Utils_Rule
::$ruleName($dateVal);
2073 // format date and time to default.
2074 if ($ruleName == 'dateTime') {
2075 $dateVal = CRM_Utils_Date
::customFormat(preg_replace("/(:|\s)?/", "", $dateVal), '%Y%m%d%H%i');
2076 // hack to add seconds
2079 $formattedDate = $dateVal;
2083 return $formattedDate;
2087 * Function to return days of the month.
2091 public static function getCalendarDayOfMonth() {
2093 for ($i = 1; $i <= 31; $i++
) {
2096 $month[$i] = $i . ' / Last day of month';