3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
21 class CRM_Utils_Date
{
24 * Format a date by padding it with leading '0'.
28 * @param string $separator
29 * The separator to use when formatting the date.
30 * @param int|string $invalidDate what to return if the date is invalid
33 * formatted string for date
35 public static function format($date, $separator = '', $invalidDate = 0) {
36 if (is_numeric($date) &&
37 ((strlen($date) == 8) ||
(strlen($date) == 14))
42 if (!is_array($date) ||
43 CRM_Utils_System
::isNull($date) ||
49 $date['Y'] = (int ) $date['Y'];
50 if ($date['Y'] < 1000 ||
$date['Y'] > 2999) {
54 if (array_key_exists('m', $date)) {
55 $date['M'] = $date['m'];
57 elseif (array_key_exists('F', $date)) {
58 $date['M'] = $date['F'];
61 if (!empty($date['M'])) {
62 $date['M'] = (int ) $date['M'];
63 if ($date['M'] < 1 ||
$date['M'] > 12) {
71 if (!empty($date['d'])) {
72 $date['d'] = (int ) $date['d'];
78 if (!checkdate($date['M'], $date['d'], $date['Y'])) {
82 $date['M'] = sprintf('%02d', $date['M']);
83 $date['d'] = sprintf('%02d', $date['d']);
86 if (CRM_Utils_Array
::value('H', $date) != NULL ||
87 CRM_Utils_Array
::value('h', $date) != NULL ||
88 CRM_Utils_Array
::value('i', $date) != NULL ||
89 CRM_Utils_Array
::value('s', $date) != NULL
92 if (!empty($date['h'])) {
93 if (CRM_Utils_Array
::value('A', $date) == 'PM' or CRM_Utils_Array
::value('a', $date) == 'pm') {
94 if ($date['h'] != 12) {
95 $date['h'] = $date['h'] +
12;
98 if ((CRM_Utils_Array
::value('A', $date) == 'AM' or CRM_Utils_Array
::value('a', $date) == 'am') &&
99 CRM_Utils_Array
::value('h', $date) == 12
104 $date['h'] = (int ) $date['h'];
110 // in 24-hour format the hour is under the 'H' key
111 if (!empty($date['H'])) {
112 $date['H'] = (int) $date['H'];
118 if (!empty($date['i'])) {
119 $date['i'] = (int ) $date['i'];
125 if ($date['h'] == 0 && $date['H'] != 0) {
126 $date['h'] = $date['H'];
129 if (!empty($date['s'])) {
130 $date['s'] = (int ) $date['s'];
136 $date['h'] = sprintf('%02d', $date['h']);
137 $date['i'] = sprintf('%02d', $date['i']);
138 $date['s'] = sprintf('%02d', $date['s']);
143 $time .= $date['h'] . $separator . $date['i'] . $separator . $date['s'];
146 return $date['Y'] . $separator . $date['M'] . $separator . $date['d'] . $time;
150 * Return abbreviated weekday names according to the locale.
152 * Array will be in localized order according to 'weekBegins' setting,
153 * but array keys will always match to:
159 * 0-based array with abbreviated weekday names
162 public static function getAbbrWeekdayNames() {
163 $key = 'abbrDays_' . \CRM_Core_I18n
::getLocale();
164 if (empty(\Civi
::$statics[__CLASS__
][$key])) {
165 $intl_formatter = IntlDateFormatter
::create(CRM_Core_I18n
::getLocale(), IntlDateFormatter
::MEDIUM
, IntlDateFormatter
::MEDIUM
, NULL, IntlDateFormatter
::GREGORIAN
, 'E');
167 0 => $intl_formatter->format(strtotime('Sunday')),
168 1 => $intl_formatter->format(strtotime('Monday')),
169 2 => $intl_formatter->format(strtotime('Tuesday')),
170 3 => $intl_formatter->format(strtotime('Wednesday')),
171 4 => $intl_formatter->format(strtotime('Thursday')),
172 5 => $intl_formatter->format(strtotime('Friday')),
173 6 => $intl_formatter->format(strtotime('Saturday')),
175 // First day of the week
176 $firstDay = Civi
::settings()->get('weekBegins');
178 \Civi
::$statics[__CLASS__
][$key] = [];
179 for ($i = $firstDay; count(\Civi
::$statics[__CLASS__
][$key]) < 7; $i = $i > 5 ?
0 : $i +
1) {
180 \Civi
::$statics[__CLASS__
][$key][$i] = $days[$i];
183 return \Civi
::$statics[__CLASS__
][$key];
187 * Return full weekday names according to the locale.
189 * Array will be in localized order according to 'weekBegins' setting,
190 * but array keys will always match to:
196 * 0-based array with full weekday names
199 public static function getFullWeekdayNames() {
200 $key = 'fullDays_' . \CRM_Core_I18n
::getLocale();
201 if (empty(\Civi
::$statics[__CLASS__
][$key])) {
202 $intl_formatter = IntlDateFormatter
::create(CRM_Core_I18n
::getLocale(), IntlDateFormatter
::MEDIUM
, IntlDateFormatter
::MEDIUM
, NULL, IntlDateFormatter
::GREGORIAN
, 'EEEE');
204 0 => $intl_formatter->format(strtotime('Sunday')),
205 1 => $intl_formatter->format(strtotime('Monday')),
206 2 => $intl_formatter->format(strtotime('Tuesday')),
207 3 => $intl_formatter->format(strtotime('Wednesday')),
208 4 => $intl_formatter->format(strtotime('Thursday')),
209 5 => $intl_formatter->format(strtotime('Friday')),
210 6 => $intl_formatter->format(strtotime('Saturday')),
212 // First day of the week
213 $firstDay = Civi
::settings()->get('weekBegins');
215 \Civi
::$statics[__CLASS__
][$key] = [];
216 for ($i = $firstDay; count(\Civi
::$statics[__CLASS__
][$key]) < 7; $i = $i > 5 ?
0 : $i +
1) {
217 \Civi
::$statics[__CLASS__
][$key][$i] = $days[$i];
220 return \Civi
::$statics[__CLASS__
][$key];
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 $key = 'abbrMonthNames_' . \CRM_Core_I18n
::getLocale();
234 if (empty(\Civi
::$statics[__CLASS__
][$key])) {
235 $intl_formatter = IntlDateFormatter
::create(CRM_Core_I18n
::getLocale(), IntlDateFormatter
::MEDIUM
, IntlDateFormatter
::MEDIUM
, NULL, IntlDateFormatter
::GREGORIAN
, 'MMM');
236 \Civi
::$statics[__CLASS__
][$key] = [
237 1 => $intl_formatter->format(strtotime('January')),
238 2 => $intl_formatter->format(strtotime('February')),
239 3 => $intl_formatter->format(strtotime('March')),
240 4 => $intl_formatter->format(strtotime('April')),
241 5 => $intl_formatter->format(strtotime('May')),
242 6 => $intl_formatter->format(strtotime('June')),
243 7 => $intl_formatter->format(strtotime('July')),
244 8 => $intl_formatter->format(strtotime('August')),
245 9 => $intl_formatter->format(strtotime('September')),
246 10 => $intl_formatter->format(strtotime('October')),
247 11 => $intl_formatter->format(strtotime('November')),
248 12 => $intl_formatter->format(strtotime('December')),
252 return \Civi
::$statics[__CLASS__
][$key][$month];
254 return \Civi
::$statics[__CLASS__
][$key];
258 * Return full month names according to the locale.
261 * 1-based array with full month names
264 public static function &getFullMonthNames() {
265 $key = 'fullMonthNames_' . \CRM_Core_I18n
::getLocale();
266 if (empty(\Civi
::$statics[__CLASS__
][$key])) {
267 // Not relying on strftime because it depends on the operating system
268 // and most people will not have a non-US locale configured out of the box
269 // Ignoring other date names for now, since less visible by default
270 \Civi
::$statics[__CLASS__
][$key] = [
279 9 => ts('September'),
281 11 => ts('November'),
282 12 => ts('December'),
286 return \Civi
::$statics[__CLASS__
][$key];
290 * @param string $string
294 public static function unixTime($string) {
295 if (empty($string)) {
298 $parsedDate = date_parse($string);
299 return mktime(CRM_Utils_Array
::value('hour', $parsedDate),
300 CRM_Utils_Array
::value('minute', $parsedDate),
302 CRM_Utils_Array
::value('month', $parsedDate),
303 CRM_Utils_Array
::value('day', $parsedDate),
304 CRM_Utils_Array
::value('year', $parsedDate)
309 * Create a date and time string in a provided format.
310 * %A - Full day name ('Saturday'..'Sunday')
311 * %a - abbreviated day name ('Sat'..'Sun')
312 * %b - abbreviated month name ('Jan'..'Dec')
313 * %B - full month name ('January'..'December')
314 * %d - day of the month as a decimal number, 0-padded ('01'..'31')
315 * %e - day of the month as a decimal number, blank-padded (' 1'..'31')
316 * %E - day of the month as a decimal number ('1'..'31')
317 * %f - English ordinal suffix for the day of the month ('st', 'nd', 'rd', 'th')
318 * %H - hour in 24-hour format, 0-padded ('00'..'23')
319 * %I - hour in 12-hour format, 0-padded ('01'..'12')
320 * %k - hour in 24-hour format, blank-padded (' 0'..'23')
321 * %l - hour in 12-hour format, blank-padded (' 1'..'12')
322 * %m - month as a decimal number, 0-padded ('01'..'12')
323 * %M - minute, 0-padded ('00'..'60')
324 * %p - lowercase ante/post meridiem ('am', 'pm')
325 * %P - uppercase ante/post meridiem ('AM', 'PM')
326 * %Y - year as a decimal number including the century ('2005')
328 * @param string $dateString
329 * Date and time in 'YYYY-MM-DD hh:mm:ss' format.
330 * @param string $format
332 * @param array $dateParts
333 * An array with the desired date parts.
336 * the $format-formatted $date
338 public static function customFormat($dateString, $format = NULL, $dateParts = NULL) {
339 // 1-based (January) month names arrays
340 $abbrMonths = self
::getAbbrMonthNames();
341 $fullMonths = self
::getFullMonthNames();
342 $fullWeekdayNames = self
::getFullWeekdayNames();
343 $abbrWeekdayNames = self
::getAbbrWeekdayNames();
345 // backwards compatability with %D being the equivilant of %m/%d/%y
346 $format = str_replace('%D', '%m/%d/%y', $format);
349 $config = CRM_Core_Config
::singleton();
352 if (array_intersect(['h', 'H'], $dateParts)) {
353 $format = $config->dateformatDatetime
;
355 elseif (array_intersect(['d', 'j'], $dateParts)) {
356 $format = $config->dateformatFull
;
358 elseif (array_intersect(['m', 'M'], $dateParts)) {
359 $format = $config->dateformatPartial
;
362 $format = $config->dateformatYear
;
366 if (strpos(($dateString ??
''), '-')) {
367 $month = (int) substr($dateString, 5, 2);
368 $day = (int) substr($dateString, 8, 2);
371 $month = (int) substr(($dateString ??
''), 4, 2);
372 $day = (int) substr(($dateString ??
''), 6, 2);
375 if (strlen(($dateString ??
'')) > 10) {
376 $format = $config->dateformatDatetime
;
379 $format = $config->dateformatFull
;
381 elseif ($month > 0) {
382 $format = $config->dateformatPartial
;
385 $format = $config->dateformatYear
;
390 if (!CRM_Utils_System
::isNull($dateString)) {
391 if (strpos($dateString, '-')) {
392 $year = (int) substr($dateString, 0, 4);
393 $month = (int) substr($dateString, 5, 2);
394 $day = (int) substr($dateString, 8, 2);
396 $hour24 = (int) substr($dateString, 11, 2);
397 $minute = (int) substr($dateString, 14, 2);
398 $second = (int) substr($dateString, 17, 2);
401 $year = (int) substr($dateString, 0, 4);
402 $month = (int) substr($dateString, 4, 2);
403 $day = (int) substr($dateString, 6, 2);
405 $hour24 = (int) substr($dateString, 8, 2);
406 $minute = (int) substr($dateString, 10, 2);
407 $second = (int) substr($dateString, 12, 2);
410 $dayInt = date('w', strtotime($dateString));
412 if ($day %
10 == 1 and $day != 11) {
415 elseif ($day %
10 == 2 and $day != 12) {
418 elseif ($day %
10 == 3 and $day != 13) {
439 $hour12 = $hour24 - 12;
445 '%A' => $fullWeekdayNames[$dayInt] ??
NULL,
446 '%a' => $abbrWeekdayNames[$dayInt] ??
NULL,
447 '%b' => $abbrMonths[$month] ??
NULL,
448 '%B' => $fullMonths[$month] ??
NULL,
449 '%d' => $day > 9 ?
$day : '0' . $day,
450 '%e' => $day > 9 ?
$day : ' ' . $day,
453 '%H' => $hour24 > 9 ?
$hour24 : '0' . $hour24,
454 '%h' => $hour12 > 9 ?
$hour12 : '0' . $hour12,
455 '%I' => $hour12 > 9 ?
$hour12 : '0' . $hour12,
456 '%k' => $hour24 > 9 ?
$hour24 : ' ' . $hour24,
457 '%l' => $hour12 > 9 ?
$hour12 : ' ' . $hour12,
458 '%m' => $month > 9 ?
$month : '0' . $month,
459 '%M' => $minute > 9 ?
$minute : '0' . $minute,
460 '%i' => $minute > 9 ?
$minute : '0' . $minute,
461 '%p' => strtolower($type),
464 '%y' => substr($year, 2),
465 '%s' => str_pad($second, 2, 0, STR_PAD_LEFT
),
466 '%S' => str_pad($second, 2, 0, STR_PAD_LEFT
),
467 '%Z' => date('T', strtotime($dateString)),
470 return strtr($format, $date);
476 * Format the field according to the site's preferred date format.
478 * This is likely to look something like December 31st, 2020.
480 * @param string $date
484 public static function formatDateOnlyLong(string $date):string {
485 return CRM_Utils_Date
::customFormat($date, Civi
::settings()->get('dateformatFull'));
489 * Wrapper for customFormat that takes a timestamp
491 * @param int $timestamp
492 * Date and time in timestamp format.
493 * @param string $format
495 * @param array $dateParts
496 * An array with the desired date parts.
499 * the $format-formatted $date
501 public static function customFormatTs($timestamp, $format = NULL, $dateParts = NULL) {
502 return CRM_Utils_Date
::customFormat(date("Y-m-d H:i:s", $timestamp), $format, $dateParts);
506 * Converts the date/datetime from MySQL format to ISO format
508 * @param string $mysql
509 * Date/datetime in MySQL format.
512 * date/datetime in ISO format
514 public static function mysqlToIso($mysql) {
515 $year = substr(($mysql ??
''), 0, 4);
516 $month = substr(($mysql ??
''), 4, 2);
517 $day = substr(($mysql ??
''), 6, 2);
518 $hour = substr(($mysql ??
''), 8, 2);
519 $minute = substr(($mysql ??
''), 10, 2);
520 $second = substr(($mysql ??
''), 12, 2);
546 * Converts the date/datetime from ISO format to MySQL format
547 * Note that until CRM-14986/ 4.4.7 this was required whenever the pattern $dao->find(TRUE): $dao->save(); was
548 * used to update an object with a date field was used. The DAO now checks for a '-' in date field strings
549 * & runs this function if the - appears - meaning it is likely redundant in the form & BAO layers
552 * Date/datetime in ISO format.
555 * date/datetime in MySQL format
557 public static function isoToMysql($iso) {
558 $dropArray = ['-' => '', ':' => '', ' ' => ''];
559 return strtr(($iso ??
''), $dropArray);
563 * Converts the any given date to default date format.
565 * @param array $params
566 * Has given date-format.
567 * @param int $dateType
569 * @param string $dateParam
574 public static function convertToDefaultDate(&$params, $dateType, $dateParam) {
578 if (!empty($params[$dateParam])) {
579 // suppress hh:mm or hh:mm:ss if it exists CRM-7957
580 $value = preg_replace("/(\s(([01]\d)|[2][0-3])(:([0-5]\d)){1,2})$/", "", $params[$dateParam]);
585 if (!preg_match('/^\d\d\d\d-?(\d|\d\d)-?(\d|\d\d)$/', $value)) {
591 if (!preg_match('/^(\d|\d\d)[-\/](\d|\d\d)[-\/]\d\d$/', $value)) {
597 if (!preg_match('/^(\d|\d\d)[-\/](\d|\d\d)[-\/]\d\d\d\d$/', $value)) {
603 if (!preg_match('/^[A-Za-z]*.[ \t]?\d\d\,[ \t]?\d\d\d\d$/', $value)) {
609 if (!preg_match('/^\d\d-[A-Za-z]{3}.*-\d\d$/', $value) && !preg_match('/^\d\d[-\/]\d\d[-\/]\d\d$/', $value)) {
615 if (!preg_match('/^(\d|\d\d)[-\/](\d|\d\d)[-\/]\d\d\d\d/', $value)) {
621 if ($dateType == 1) {
622 $formattedDate = explode("-", $value);
623 if (count($formattedDate) == 3) {
624 $year = (int) $formattedDate[0];
625 $month = (int) $formattedDate[1];
626 $day = (int) $formattedDate[2];
628 elseif (count($formattedDate) == 1 && (strlen($value) == 8)) {
636 if ($dateType == 2 ||
$dateType == 4) {
637 $formattedDate = explode("/", $value);
638 if (count($formattedDate) != 3) {
639 $formattedDate = explode("-", $value);
641 if (count($formattedDate) == 3) {
642 $year = (int) $formattedDate[2];
643 $month = (int) $formattedDate[0];
644 $day = (int) $formattedDate[1];
650 if ($dateType == 8) {
651 $dateArray = explode(' ', $value);
653 $dateArray[1] = (int) substr($dateArray[1], 0, 2);
656 $fullMonths = self
::getFullMonthNames();
657 foreach ($fullMonths as $key => $val) {
658 if (strtolower($dateArray[0]) == strtolower($val)) {
664 $abbrMonths = self
::getAbbrMonthNames();
665 foreach ($abbrMonths as $key => $val) {
666 if (strtolower(trim($dateArray[0], ".")) == strtolower($val)) {
672 $year = (int) $dateArray[2];
673 $day = (int) $dateArray[1];
674 $month = (int) $monthInt;
676 if ($dateType == 16) {
677 $dateArray = explode('-', $value);
678 if (count($dateArray) != 3) {
679 $dateArray = explode('/', $value);
682 if (count($dateArray) == 3) {
684 $fullMonths = self
::getFullMonthNames();
685 foreach ($fullMonths as $key => $val) {
686 if (strtolower($dateArray[1]) == strtolower($val)) {
692 $abbrMonths = self
::getAbbrMonthNames();
693 foreach ($abbrMonths as $key => $val) {
694 if (strtolower(trim($dateArray[1], ".")) == strtolower($val)) {
701 $monthInt = $dateArray[1];
704 $year = (int) $dateArray[2];
705 $day = (int) $dateArray[0];
706 $month = (int) $monthInt;
712 if ($dateType == 32) {
713 $formattedDate = explode("/", $value);
714 if (count($formattedDate) == 3) {
715 $year = (int) $formattedDate[2];
716 $month = (int) $formattedDate[1];
717 $day = (int) $formattedDate[0];
724 $month = ($month < 10) ?
"0" . "$month" : $month;
725 $day = ($day < 10) ?
"0" . "$day" : $day;
729 $year = substr($now['year'], 0, 2) * 100 +
$year;
730 if ($year > ($now['year'] +
5)) {
733 elseif ($year <= ($now['year'] - 95)) {
738 if ($params[$dateParam]) {
739 $params[$dateParam] = "$year$month$day";
741 // if month is invalid return as error
742 if ($month !== '00' && $month <= 12) {
749 * Translate a TTL to a concrete expiration time.
751 * @param null|int|DateInterval $ttl
752 * @param int $default
753 * The value to use if $ttl is not specified (NULL).
755 * Timestamp (seconds since epoch).
756 * @throws \CRM_Utils_Cache_InvalidArgumentException
758 public static function convertCacheTtlToExpires($ttl, $default) {
764 return time() +
$ttl;
766 elseif ($ttl instanceof DateInterval
) {
767 return date_add(new DateTime(), $ttl)->getTimestamp();
770 throw new CRM_Utils_Cache_InvalidArgumentException("Invalid cache TTL");
777 * @param null|int|DateInterval $ttl
778 * @param int $default
779 * The value to use if $ttl is not specified (NULL).
781 * Seconds until expiration.
782 * @throws \CRM_Utils_Cache_InvalidArgumentException
784 public static function convertCacheTtl($ttl, $default) {
788 elseif (is_int($ttl)) {
791 elseif ($ttl instanceof DateInterval
) {
792 return date_add(new DateTime(), $ttl)->getTimestamp() - time();
795 throw new CRM_Utils_Cache_InvalidArgumentException("Invalid cache TTL");
800 * @param int|false|null $timeStamp
802 * @return bool|string
804 public static function currentDBDate($timeStamp = NULL) {
805 return $timeStamp ?
date('YmdHis', $timeStamp) : date('YmdHis');
814 public static function overdue($date, $now = NULL) {
815 $mysqlDate = self
::isoToMysql($date);
817 $now = self
::currentDBDate();
820 $now = self
::isoToMysql($now);
823 return !(strtotime($mysqlDate) >= strtotime($now));
827 * Get customized today.
829 * This function is used for getting customized today. To get
830 * actuall today pass 'dayParams' as null. or else pass the day,
831 * month, year values as array values
832 * Example: $dayParams = array(
833 * 'day' => '25', 'month' => '10',
834 * 'year' => '2007' );
836 * @param array $dayParams of the day, month, year.
837 * Array of the day, month, year.
839 * @param string $format
840 * Expected date format( default.
841 * format is 2007-12-21 )
844 * Return the customized today's date (Y-m-d)
846 public static function getToday($dayParams = NULL, $format = "Y-m-d") {
847 if (is_null($dayParams) ||
empty($dayParams)) {
848 $today = date($format);
851 $today = date($format, mktime(0, 0, 0,
862 * Find whether today's date lies in
865 * @param date $startDate
866 * Start date for the range.
867 * @param date $endDate
868 * End date for the range.
871 * true if today's date is in the given date range
873 public static function getRange($startDate, $endDate) {
874 $today = date("Y-m-d");
875 $mysqlStartDate = self
::isoToMysql($startDate);
876 $mysqlEndDate = self
::isoToMysql($endDate);
877 $mysqlToday = self
::isoToMysql($today);
879 if ((isset($mysqlStartDate) && isset($mysqlEndDate)) && (($mysqlToday >= $mysqlStartDate) && ($mysqlToday <= $mysqlEndDate))) {
882 elseif ((isset($mysqlStartDate) && !isset($mysqlEndDate)) && (($mysqlToday >= $mysqlStartDate))) {
885 elseif ((!isset($mysqlStartDate) && isset($mysqlEndDate)) && (($mysqlToday <= $mysqlEndDate))) {
892 * Get start date and end from
893 * the given relative term and unit
895 * @param string $relative Relative format in the format term.unit.
898 * @param string $from
900 * @param string $fromTime
901 * @param string $toTime
904 * start date, end date
906 public static function getFromTo($relative, $from = NULL, $to = NULL, $fromTime = NULL, $toTime = '235959') {
908 list($term, $unit) = explode('.', $relative, 2);
909 $dateRange = self
::relativeToAbsolute($term, $unit);
910 $from = substr(($dateRange['from'] ??
''), 0, 8);
911 $to = substr(($dateRange['to'] ??
''), 0, 8);
912 // @todo fix relativeToAbsolute & add tests
913 // relativeToAbsolute returns 8 char date strings
914 // or 14 char date + time strings.
915 // We should use those. However, it turns out to be unreliable.
916 // e.g. this.week does NOT return 235959 for 'from'
917 // so our defaults are more reliable.
918 // Currently relativeToAbsolute only supports 'whole' days so that is ok
921 $from = self
::processDate($from, $fromTime);
922 $to = self
::processDate($to, $toTime);
928 * Calculate Age in Years if greater than one year else in months.
930 * @param date $birthDate
932 * @param date $targetDate
933 * Target Date. (show age on specific date)
936 * array $results contains years or months
938 public static function calculateAge($birthDate, $targetDate = NULL) {
940 $formatedBirthDate = CRM_Utils_Date
::customFormat($birthDate, '%Y-%m-%d');
942 $bDate = explode('-', $formatedBirthDate);
943 $birthYear = $bDate[0];
944 $birthMonth = $bDate[1];
945 $birthDay = $bDate[2];
946 $targetDate = strtotime($targetDate ??
date('Y-m-d'));
948 $year_diff = date("Y", $targetDate) - $birthYear;
950 // don't calculate age CRM-3143
951 if ($birthYear == '1902') {
954 switch ($year_diff) {
956 $month = (12 - $birthMonth) +
date("m", $targetDate);
958 if (date("d", $targetDate) < $birthDay) {
961 $results['months'] = $month;
963 elseif ($month == 12 && (date("d", $targetDate) < $birthDay)) {
964 $results['months'] = $month - 1;
967 $results['years'] = $year_diff;
972 $month = date("m", $targetDate) - $birthMonth;
973 $results['months'] = $month;
977 $results['years'] = $year_diff;
978 if ((date("m", $targetDate) < $birthMonth) ||
(date("m", $targetDate) == $birthMonth) && (date("d", $targetDate) < $birthDay)) {
987 * Calculate next payment date according to provided unit & interval
989 * @param string $unit
990 * Frequency unit like year,month, week etc.
992 * @param int $interval
993 * Frequency interval.
996 * Start date of pledge.
998 * @param bool $dontCareTime
1001 * contains new date with added interval
1003 public static function intervalAdd($unit, $interval, $date, $dontCareTime = FALSE) {
1004 if (is_array($date)) {
1005 $hour = $date['H'] ??
'00';
1006 $minute = $date['i'] ??
'00';
1007 $second = $date['s'] ??
'00';
1008 $month = $date['M'] ??
NULL;
1009 $day = $date['d'] ??
NULL;
1010 $year = $date['Y'] ??
NULL;
1013 extract(date_parse($date));
1015 $date = mktime($hour, $minute, $second, $month, $day, $year);
1018 $date = mktime($hour, $minute, $second, $month, $day, $year +
$interval);
1022 $date = mktime($hour, $minute, $second, $month +
$interval, $day, $year);
1026 $interval = $interval * 7;
1027 $date = mktime($hour, $minute, $second, $month, $day +
$interval, $year);
1031 $date = mktime($hour, $minute, $second, $month, $day +
$interval, $year);
1035 $date = mktime($hour, $minute, $second +
$interval, $month, $day, $year);
1039 $scheduleDate = explode("-", date("n-j-Y-H-i-s", $date));
1042 $date['M'] = $scheduleDate[0];
1043 $date['d'] = $scheduleDate[1];
1044 $date['Y'] = $scheduleDate[2];
1045 if ($dontCareTime == FALSE) {
1046 $date['H'] = $scheduleDate[3];
1047 $date['i'] = $scheduleDate[4];
1048 $date['s'] = $scheduleDate[5];
1054 * Get the smarty view presentation mapping for the given format.
1056 * Historically it was decided that where the view format is 'dd/mm/yy' or 'mm/dd/yy'
1057 * they should be rendered using a longer date format. This is likely as much to
1058 * do with the earlier date widget being unable to handle some formats as usablity.
1059 * However, we continue to respect this.
1062 * Given format ( eg 'M Y', 'Y M' ).
1064 * @return string|null
1065 * Smarty translation of the date format. Null is also valid and is translated
1066 * according to the available parts at the smarty layer.
1068 public static function getDateFieldViewFormat($format) {
1069 $supportableFormats = [
1070 'mm/dd' => '%B %E%f',
1071 'dd-mm' => '%E%f %B',
1075 'dd/mm/yy' => '%E%f %B %Y',
1078 return array_key_exists($format, $supportableFormats) ?
$supportableFormats[$format] : self
::pickBestSmartyFormat($format);
1082 * Pick the smarty format from settings that best matches the time string we have.
1084 * For view purposes we historically use the setting that most closely matches the data
1085 * in the format from our settings, as opposed to the setting configured for the field.
1090 public static function pickBestSmartyFormat($format) {
1091 if (stristr($format, 'h')) {
1092 return Civi
::settings()->get('dateformatDatetime');
1094 if (stristr($format, 'd') ||
stristr($format, 'j')) {
1095 return Civi
::settings()->get('dateformatFull');
1097 if (stristr($format, 'm')) {
1098 return Civi
::settings()->get('dateformatPartial');
1100 return Civi
::settings()->get('dateformatYear');
1104 * Map date plugin and actual format that is used by PHP.
1108 public static function datePluginToPHPFormats() {
1109 $dateInputFormats = [
1110 "mm/dd/yy" => 'm/d/Y',
1111 "dd/mm/yy" => 'd/m/Y',
1112 "yy-mm-dd" => 'Y-m-d',
1113 "dd-mm-yy" => 'd-m-Y',
1114 "dd.mm.yy" => 'd.m.Y',
1116 "M d, yy" => 'M j, Y',
1117 "d M yy" => 'j M Y',
1118 "MM d, yy" => 'F j, Y',
1119 "d MM yy" => 'j F Y',
1120 "DD, d MM yy" => 'l, j F Y',
1128 return $dateInputFormats;
1132 * Resolves the given relative time interval into finite time limits.
1134 * @param string $relativeTerm
1135 * Relative time frame: this, previous, previous_1.
1137 * Frequency unit like year, month, week etc.
1140 * start date and end date for the relative time frame
1142 public static function relativeToAbsolute($relativeTerm, $unit) {
1144 $from = $to = $dateRange = [];
1145 $from['H'] = $from['i'] = $from['s'] = 0;
1146 $relativeTermParts = explode('_', $relativeTerm);
1147 $relativeTermPrefix = $relativeTermParts[0];
1148 $relativeTermSuffix = $relativeTermParts[1] ??
'';
1152 switch ($relativeTerm) {
1154 $from['M'] = $from['d'] = 1;
1157 $to['Y'] = $from['Y'] = $now['year'] - 1;
1160 case 'previous_before':
1161 $from['M'] = $from['d'] = 1;
1164 $to['Y'] = $from['Y'] = $now['year'] - 2;
1168 $from['M'] = $from['d'] = 1;
1171 $from['Y'] = $now['year'] - 2;
1172 $to['Y'] = $now['year'] - 1;
1178 $to['Y'] = $now['year'] - 1;
1183 $from['M'] = $from['d'] = 1;
1184 $from['Y'] = $now['year'];
1188 case 'greater_previous':
1191 $from['Y'] = $now['year'] - 1;
1196 $to['d'] = $now['mday'];
1197 $to['M'] = $now['mon'];
1198 $to['Y'] = $now['year'];
1200 $to['i'] = $to['s'] = 59;
1201 $from = self
::intervalAdd('year', -1, $to);
1202 $from = self
::intervalAdd('second', 1, $from);
1206 $from['M'] = $from['d'] = 1;
1207 $from['Y'] = $now['year'];
1209 $to['i'] = $to['s'] = 59;
1210 $to['d'] = $now['mday'];
1211 $to['M'] = $now['mon'];
1212 $to['Y'] = $now['year'];
1216 $to['d'] = $now['mday'];
1217 $to['M'] = $now['mon'];
1218 $to['Y'] = $now['year'];
1220 $to['i'] = $to['s'] = 59;
1221 $from = self
::intervalAdd('year', -2, $to);
1222 $from = self
::intervalAdd('second', 1, $from);
1226 $to['d'] = $now['mday'];
1227 $to['M'] = $now['mon'];
1228 $to['Y'] = $now['year'];
1230 $to['i'] = $to['s'] = 59;
1231 $from = self
::intervalAdd('year', -3, $to);
1232 $from = self
::intervalAdd('second', 1, $from);
1238 $to['Y'] = $now['year'];
1243 $from['M'] = $from['d'] = 1;
1246 $to['Y'] = $from['Y'] = $now['year'] +
1;
1250 $from['d'] = $now['mday'];
1251 $from['M'] = $now['mon'];
1252 $from['Y'] = $now['year'];
1253 $to['d'] = $now['mday'] - 1;
1254 $to['M'] = $now['mon'];
1255 $to['Y'] = $now['year'] +
1;
1259 switch ($relativeTermPrefix) {
1262 $to['d'] = $now['mday'];
1263 $to['M'] = $now['mon'];
1264 $to['Y'] = $now['year'];
1266 $to['i'] = $to['s'] = 59;
1267 $from = self
::intervalAdd('year', -$relativeTermSuffix, $to);
1268 $from = self
::intervalAdd('second', 1, $from);
1272 $from['d'] = $from['M'] = 1;
1275 $to['Y'] = $from['Y'] = $now['year'];
1276 if (is_numeric($relativeTermSuffix)) {
1277 $from['Y'] -= ($relativeTermSuffix - 1);
1286 $config = CRM_Core_Config
::singleton();
1287 $from['d'] = $config->fiscalYearStart
['d'];
1288 $from['M'] = $config->fiscalYearStart
['M'];
1289 $fYear = self
::calculateFiscalYear($from['d'], $from['M']);
1290 switch ($relativeTermPrefix) {
1292 $from['Y'] = $fYear;
1293 $fiscalYear = mktime(0, 0, 0, $from['M'], $from['d'] - 1, $from['Y'] +
1);
1294 $fiscalEnd = explode('-', date("Y-m-d", $fiscalYear));
1295 $to['d'] = $fiscalEnd['2'];
1296 $to['M'] = $fiscalEnd['1'];
1297 $to['Y'] = $fiscalEnd['0'];
1299 $to['i'] = $to['s'] = 59;
1300 if (is_numeric($relativeTermSuffix)) {
1301 $from = self
::intervalAdd('year', (-$relativeTermSuffix), $to);
1302 $from = self
::intervalAdd('second', 1, $from);
1307 if (!is_numeric($relativeTermSuffix)) {
1308 $from['Y'] = ($relativeTermSuffix === 'before') ?
$fYear - 2 : $fYear - 1;
1309 $fiscalYear = mktime(0, 0, 0, $from['M'], $from['d'] - 1, $from['Y'] +
1);
1310 $fiscalEnd = explode('-', date("Y-m-d", $fiscalYear));
1311 $to['d'] = $fiscalEnd['2'];
1312 $to['M'] = $fiscalEnd['1'];
1313 $to['Y'] = $fiscalEnd['0'];
1315 $to['i'] = $to['s'] = 59;
1318 $from['Y'] = $fYear - $relativeTermSuffix;
1319 $fiscalYear = mktime(0, 0, 0, $from['M'], $from['d'] - 1, $from['Y'] +
1);
1320 $fiscalEnd = explode('-', date("Y-m-d", $fiscalYear));
1321 $to['d'] = $fiscalEnd['2'];
1322 $to['M'] = $fiscalEnd['1'];
1325 $to['i'] = $to['s'] = 59;
1330 $from['Y'] = $fYear +
1;
1331 $fiscalYear = mktime(0, 0, 0, $from['M'], $from['d'] - 1, $from['Y'] +
1);
1332 $fiscalEnd = explode('-', date("Y-m-d", $fiscalYear));
1333 $to['d'] = $fiscalEnd['2'];
1334 $to['M'] = $fiscalEnd['1'];
1335 $to['Y'] = $fiscalEnd['0'];
1341 switch ($relativeTerm) {
1344 $quarter = ceil($now['mon'] / 3);
1346 $from['M'] = (3 * $quarter) - 2;
1347 $to['M'] = 3 * $quarter;
1348 $to['Y'] = $from['Y'] = $now['year'];
1349 $to['d'] = date('t', mktime(0, 0, 0, $to['M'], 1, $now['year']));
1354 $quarter = ceil($now['mon'] / 3);
1355 $quarter = $quarter - $difference;
1357 if ($quarter <= 0) {
1362 $from['M'] = (3 * $quarter) - 2;
1363 $to['M'] = 3 * $quarter;
1364 $to['Y'] = $from['Y'] = $now['year'] - $subtractYear;
1365 $to['d'] = date('t', mktime(0, 0, 0, $to['M'], 1, $to['Y']));
1368 case 'previous_before':
1370 $quarter = ceil($now['mon'] / 3);
1371 $quarter = $quarter - $difference;
1373 if ($quarter <= 0) {
1378 $from['M'] = (3 * $quarter) - 2;
1379 $to['M'] = 3 * $quarter;
1380 $to['Y'] = $from['Y'] = $now['year'] - $subtractYear;
1381 $to['d'] = date('t', mktime(0, 0, 0, $to['M'], 1, $to['Y']));
1386 $quarter = ceil($now['mon'] / 3);
1387 $current_quarter = $quarter;
1388 $quarter = $quarter - $difference;
1390 if ($quarter <= 0) {
1395 $from['M'] = (3 * $quarter) - 2;
1396 switch ($current_quarter) {
1398 $to['M'] = (4 * $quarter);
1402 $to['M'] = (4 * $quarter) +
3;
1406 $to['M'] = (4 * $quarter) +
2;
1410 $to['M'] = (4 * $quarter) +
1;
1413 $to['Y'] = $from['Y'] = $now['year'] - $subtractYear;
1414 if ($to['M'] > 12) {
1415 $to['M'] = 3 * ($quarter - 3);
1416 $to['Y'] = $now['year'];
1418 $to['d'] = date('t', mktime(0, 0, 0, $to['M'], 1, $to['Y']));
1422 $quarter = ceil($now['mon'] / 3) - 1;
1424 if ($quarter <= 0) {
1428 $to['M'] = 3 * $quarter;
1429 $to['Y'] = $from['Y'] = $now['year'] - $subtractYear;
1430 $to['d'] = date('t', mktime(0, 0, 0, $to['M'], 1, $to['Y']));
1435 $quarter = ceil($now['mon'] / 3);
1437 $from['M'] = (3 * $quarter) - 2;
1438 $from['Y'] = $now['year'];
1442 case 'greater_previous':
1443 $quarter = ceil($now['mon'] / 3) - 1;
1445 if ($quarter <= 0) {
1449 $from['M'] = 3 * $quarter;
1450 $from['Y'] = $from['Y'] = $now['year'] - $subtractYear;
1451 $from['d'] = date('t', mktime(0, 0, 0, $from['M'], 1, $from['Y']));
1456 $to['d'] = $now['mday'];
1457 $to['M'] = $now['mon'];
1458 $to['Y'] = $now['year'];
1460 $to['i'] = $to['s'] = 59;
1461 $from = self
::intervalAdd('day', -90, $to);
1462 $from = self
::intervalAdd('second', 1, $from);
1466 $quarter = ceil($now['mon'] / 3);
1468 $from['M'] = (3 * $quarter) - 2;
1469 $from['Y'] = $now['year'];
1470 $to['d'] = $now['mday'];
1471 $to['M'] = $now['mon'];
1472 $to['Y'] = $now['year'];
1474 $to['i'] = $to['s'] = 59;
1478 $quarter = ceil($now['mon'] / 3);
1479 $to['M'] = 3 * $quarter;
1480 $to['Y'] = $now['year'];
1481 $to['d'] = date('t', mktime(0, 0, 0, $to['M'], 1, $now['year']));
1488 $quarter = ceil($now['mon'] / 3);
1489 $quarter = $quarter - $difference;
1492 $now['year'] = $now['year'] +
1;
1495 if ($quarter <= 0) {
1500 $from['M'] = (3 * $quarter) - 2;
1501 $to['M'] = 3 * $quarter;
1502 $to['Y'] = $from['Y'] = $now['year'] - $subtractYear;
1503 $to['d'] = date('t', mktime(0, 0, 0, $to['M'], 1, $to['Y']));
1507 $from['d'] = $now['mday'];
1508 $from['M'] = $now['mon'];
1509 $from['Y'] = $now['year'];
1511 $from['i'] = $to['s'] = 00;
1512 $to = self
::intervalAdd('day', 90, $from);
1513 $to = self
::intervalAdd('second', -1, $to);
1517 if ($relativeTermPrefix === 'ending') {
1518 $to['d'] = $now['mday'];
1519 $to['M'] = $now['mon'];
1520 $to['Y'] = $now['year'];
1522 $to['i'] = $to['s'] = 59;
1523 $from = self
::intervalAdd('month', -($relativeTermSuffix * 3), $to);
1524 $from = self
::intervalAdd('second', 1, $from);
1530 switch ($relativeTerm) {
1533 $to['d'] = date('t', mktime(0, 0, 0, $now['mon'], 1, $now['year']));
1534 $from['M'] = $to['M'] = $now['mon'];
1535 $from['Y'] = $to['Y'] = $now['year'];
1540 if ($now['mon'] == 1) {
1541 $from['M'] = $to['M'] = 12;
1542 $from['Y'] = $to['Y'] = $now['year'] - 1;
1545 $from['M'] = $to['M'] = $now['mon'] - 1;
1546 $from['Y'] = $to['Y'] = $now['year'];
1548 $to['d'] = date('t', mktime(0, 0, 0, $to['M'], 1, $to['Y']));
1551 case 'previous_before':
1553 if ($now['mon'] < 3) {
1554 $from['M'] = $to['M'] = 10 +
$now['mon'];
1555 $from['Y'] = $to['Y'] = $now['year'] - 1;
1558 $from['M'] = $to['M'] = $now['mon'] - 2;
1559 $from['Y'] = $to['Y'] = $now['year'];
1561 $to['d'] = date('t', mktime(0, 0, 0, $to['M'], 1, $to['Y']));
1566 if ($now['mon'] < 3) {
1567 $from['M'] = 10 +
$now['mon'];
1568 $from['Y'] = $now['year'] - 1;
1571 $from['M'] = $now['mon'] - 2;
1572 $from['Y'] = $now['year'];
1575 if ($now['mon'] == 1) {
1577 $to['Y'] = $now['year'] - 1;
1580 $to['M'] = $now['mon'] - 1;
1581 $to['Y'] = $now['year'];
1584 $to['d'] = date('t', mktime(0, 0, 0, $to['M'], 1, $to['Y']));
1588 // before end of past month
1589 if ($now['mon'] == 1) {
1591 $to['Y'] = $now['year'] - 1;
1594 $to['M'] = $now['mon'] - 1;
1595 $to['Y'] = $now['year'];
1598 $to['d'] = date('t', mktime(0, 0, 0, $to['M'], 1, $to['Y']));
1604 $from['M'] = $now['mon'];
1605 $from['Y'] = $now['year'];
1609 case 'greater_previous':
1610 // from end of past month
1611 if ($now['mon'] == 1) {
1613 $from['Y'] = $now['year'] - 1;
1616 $from['M'] = $now['mon'] - 1;
1617 $from['Y'] = $now['year'];
1620 $from['d'] = date('t', mktime(0, 0, 0, $from['M'], 1, $from['Y']));
1625 $to['d'] = $now['mday'];
1626 $to['M'] = $now['mon'];
1627 $to['Y'] = $now['year'];
1629 $to['i'] = $to['s'] = 59;
1630 $from = self
::intervalAdd('day', -60, $to);
1631 $from = self
::intervalAdd('second', 1, $from);
1635 $to['d'] = $now['mday'];
1636 $to['M'] = $now['mon'];
1637 $to['Y'] = $now['year'];
1639 $to['i'] = $to['s'] = 59;
1640 $from = self
::intervalAdd('day', -30, $to);
1641 $from = self
::intervalAdd('second', 1, $from);
1646 $from['M'] = $now['mon'];
1647 $from['Y'] = $now['year'];
1648 $to['d'] = $now['mday'];
1649 $to['M'] = $now['mon'];
1650 $to['Y'] = $now['year'];
1652 $to['i'] = $to['s'] = 59;
1657 $to['Y'] = $now['year'];
1658 $to['M'] = $now['mon'];
1659 $to['d'] = date('t', mktime(0, 0, 0, $now['mon'], 1, $now['year']));
1665 if ($now['mon'] == 12) {
1666 $from['M'] = $to['M'] = 1;
1667 $from['Y'] = $to['Y'] = $now['year'] +
1;
1670 $from['M'] = $to['M'] = $now['mon'] +
1;
1671 $from['Y'] = $to['Y'] = $now['year'];
1673 $to['d'] = date('t', mktime(0, 0, 0, $to['M'], 1, $to['Y']));
1677 $from['d'] = $now['mday'];
1678 $from['M'] = $now['mon'];
1679 $from['Y'] = $now['year'];
1681 $from['i'] = $to['s'] = 00;
1682 $to = self
::intervalAdd('day', 30, $from);
1683 $to = self
::intervalAdd('second', -1, $to);
1687 $from['d'] = $now['mday'];
1688 $from['M'] = $now['mon'];
1689 $from['Y'] = $now['year'];
1691 $from['i'] = $to['s'] = 00;
1692 $to = self
::intervalAdd('day', 60, $from);
1693 $to = self
::intervalAdd('second', -1, $to);
1697 if ($relativeTermPrefix === 'ending') {
1698 $to['d'] = $now['mday'];
1699 $to['M'] = $now['mon'];
1700 $to['Y'] = $now['year'];
1702 $to['i'] = $to['s'] = 59;
1703 $from = self
::intervalAdd($unit, -$relativeTermSuffix, $to);
1704 $from = self
::intervalAdd('second', 1, $from);
1710 $weekFirst = Civi
::settings()->get('weekBegins');
1711 $thisDay = $now['wday'];
1712 if ($weekFirst > $thisDay) {
1713 $diffDay = $thisDay - $weekFirst +
7;
1716 $diffDay = $thisDay - $weekFirst;
1718 switch ($relativeTerm) {
1720 $from['d'] = $now['mday'];
1721 $from['M'] = $now['mon'];
1722 $from['Y'] = $now['year'];
1723 $from = self
::intervalAdd('day', -1 * ($diffDay), $from);
1724 $to = self
::intervalAdd('day', 6, $from);
1728 $from['d'] = $now['mday'];
1729 $from['M'] = $now['mon'];
1730 $from['Y'] = $now['year'];
1731 $from = self
::intervalAdd('day', -1 * ($diffDay) - 7, $from);
1732 $to = self
::intervalAdd('day', 6, $from);
1735 case 'previous_before':
1736 $from['d'] = $now['mday'];
1737 $from['M'] = $now['mon'];
1738 $from['Y'] = $now['year'];
1739 $from = self
::intervalAdd('day', -1 * ($diffDay) - 14, $from);
1740 $to = self
::intervalAdd('day', 6, $from);
1744 $from['d'] = $now['mday'];
1745 $from['M'] = $now['mon'];
1746 $from['Y'] = $now['year'];
1747 $from = self
::intervalAdd('day', -1 * ($diffDay) - 14, $from);
1748 $to = self
::intervalAdd('day', 13, $from);
1752 $to['d'] = $now['mday'];
1753 $to['M'] = $now['mon'];
1754 $to['Y'] = $now['year'];
1755 $to = self
::intervalAdd('day', -1 * ($diffDay) - 1, $to);
1760 $from['d'] = $now['mday'];
1761 $from['M'] = $now['mon'];
1762 $from['Y'] = $now['year'];
1763 $from = self
::intervalAdd('day', -1 * ($diffDay), $from);
1767 case 'greater_previous':
1768 $from['d'] = $now['mday'];
1769 $from['M'] = $now['mon'];
1770 $from['Y'] = $now['year'];
1771 $from = self
::intervalAdd('day', -1 * ($diffDay) - 1, $from);
1776 $to['d'] = $now['mday'];
1777 $to['M'] = $now['mon'];
1778 $to['Y'] = $now['year'];
1780 $to['i'] = $to['s'] = 59;
1781 $from = self
::intervalAdd('day', -7, $to);
1782 $from = self
::intervalAdd('second', 1, $from);
1786 $from['d'] = $now['mday'];
1787 $from['M'] = $now['mon'];
1788 $from['Y'] = $now['year'];
1789 $from = self
::intervalAdd('day', -1 * ($diffDay), $from);
1790 $to['d'] = $now['mday'];
1791 $to['M'] = $now['mon'];
1792 $to['Y'] = $now['year'];
1794 $to['i'] = $to['s'] = 59;
1798 $to['d'] = $now['mday'];
1799 $to['M'] = $now['mon'];
1800 $to['Y'] = $now['year'];
1802 $to = self
::intervalAdd('day', -1 * ($diffDay) +
6, $to);
1807 $from['d'] = $now['mday'];
1808 $from['M'] = $now['mon'];
1809 $from['Y'] = $now['year'];
1810 $from = self
::intervalAdd('day', -1 * ($diffDay) +
7, $from);
1811 $to = self
::intervalAdd('day', 6, $from);
1815 $from['d'] = $now['mday'];
1816 $from['M'] = $now['mon'];
1817 $from['Y'] = $now['year'];
1819 $from['i'] = $to['s'] = 00;
1820 $to = self
::intervalAdd('day', 7, $from);
1821 $to = self
::intervalAdd('second', -1, $to);
1825 if ($relativeTermPrefix === 'ending') {
1826 $to['d'] = $now['mday'];
1827 $to['M'] = $now['mon'];
1828 $to['Y'] = $now['year'];
1830 $to['i'] = $to['s'] = 59;
1831 $from = self
::intervalAdd($unit, -$relativeTermSuffix, $to);
1832 $from = self
::intervalAdd('second', 1, $from);
1838 switch ($relativeTerm) {
1840 $from['d'] = $to['d'] = $now['mday'];
1841 $from['M'] = $to['M'] = $now['mon'];
1842 $from['Y'] = $to['Y'] = $now['year'];
1846 $from['d'] = $now['mday'];
1847 $from['M'] = $now['mon'];
1848 $from['Y'] = $now['year'];
1849 $from = self
::intervalAdd('day', -1, $from);
1850 $to['d'] = $from['d'];
1851 $to['M'] = $from['M'];
1852 $to['Y'] = $from['Y'];
1855 case 'previous_before':
1856 $from['d'] = $now['mday'];
1857 $from['M'] = $now['mon'];
1858 $from['Y'] = $now['year'];
1859 $from = self
::intervalAdd('day', -2, $from);
1860 $to['d'] = $from['d'];
1861 $to['M'] = $from['M'];
1862 $to['Y'] = $from['Y'];
1866 $from['d'] = $to['d'] = $now['mday'];
1867 $from['M'] = $to['M'] = $now['mon'];
1868 $from['Y'] = $to['Y'] = $now['year'];
1869 $from = self
::intervalAdd('day', -2, $from);
1870 $to = self
::intervalAdd('day', -1, $to);
1874 $to['d'] = $now['mday'];
1875 $to['M'] = $now['mon'];
1876 $to['Y'] = $now['year'];
1877 $to = self
::intervalAdd('day', -1, $to);
1882 $from['d'] = $now['mday'];
1883 $from['M'] = $now['mon'];
1884 $from['Y'] = $now['year'];
1889 $to['d'] = $now['mday'];
1890 $to['M'] = $now['mon'];
1891 $to['Y'] = $now['year'];
1892 $to = self
::intervalAdd('day', 1, $to);
1893 $from['d'] = $to['d'];
1894 $from['M'] = $to['M'];
1895 $from['Y'] = $to['Y'];
1899 if ($relativeTermPrefix === 'ending') {
1900 $to['d'] = $now['mday'];
1901 $to['M'] = $now['mon'];
1902 $to['Y'] = $now['year'];
1904 $to['i'] = $to['s'] = 59;
1905 $from = self
::intervalAdd($unit, -$relativeTermSuffix, $to);
1906 $from = self
::intervalAdd('second', 1, $from);
1912 $dateRange['from'] = empty($from) ?
NULL : self
::format($from);
1913 $dateRange['to'] = empty($to) ?
NULL : self
::format($to);
1918 * Calculate current fiscal year based on the fiscal month and day.
1920 * @param int $fyDate
1921 * Fiscal start date.
1923 * @param int $fyMonth
1924 * Fiscal Start Month.
1927 * $fy Current Fiscal Year
1929 public static function calculateFiscalYear($fyDate, $fyMonth) {
1930 $date = date("Y-m-d");
1931 $currentYear = date("Y");
1933 // recalculate the date because month 4::04 make the difference
1934 $fiscalYear = explode('-', date("Y-m-d", mktime(0, 0, 0, $fyMonth, $fyDate, $currentYear)));
1935 $fyDate = $fiscalYear[2];
1936 $fyMonth = $fiscalYear[1];
1937 $fyStartDate = date("Y-m-d", mktime(0, 0, 0, $fyMonth, $fyDate, $currentYear));
1939 if ($fyStartDate > $date) {
1940 $fy = intval(intval($currentYear) - 1);
1943 $fy = intval($currentYear);
1949 * Function to process date, convert to mysql format
1951 * @param string $date
1953 * @param string $time
1955 * @param bool|string $returnNullString 'null' needs to be returned
1956 * so that db oject will set null in db
1957 * @param string $format
1958 * Expected return date format.( default is mysql ).
1961 * date format that is excepted by mysql
1963 public static function processDate($date, $time = NULL, $returnNullString = FALSE, $format = 'YmdHis') {
1966 if ($returnNullString) {
1967 $mysqlDate = 'null';
1970 if (trim($date ??
'')) {
1971 $mysqlDate = date($format, strtotime($date . ' ' . $time));
1978 * Add the metadata about a date field to the field.
1980 * This metadata will work with the call $form->add('datepicker', ...
1982 * @param array $fieldMetaData
1983 * @param array $field
1987 public static function addDateMetadataToField($fieldMetaData, $field) {
1988 if (isset($fieldMetaData['html'])) {
1989 $field['html_type'] = $fieldMetaData['html']['type'];
1990 if ($field['html_type'] === 'Select Date') {
1991 if (!isset($field['date_format'])) {
1992 $dateAttributes = CRM_Core_SelectValues
::date($fieldMetaData['html']['formatType'], NULL, NULL, NULL, 'Input');
1993 $field['start_date_years'] = $dateAttributes['minYear'];
1994 $field['end_date_years'] = $dateAttributes['maxYear'];
1995 $field['date_format'] = $dateAttributes['format'];
1996 $field['is_datetime_field'] = TRUE;
1997 $field['time_format'] = $dateAttributes['time'];
1998 $field['smarty_view_format'] = $dateAttributes['smarty_view_format'];
2000 $field['datepicker']['extra'] = self
::getDatePickerExtra($field);
2001 $field['datepicker']['attributes'] = self
::getDatePickerAttributes($field);
2008 * Get the fields required for the 'extra' parameter when adding a datepicker.
2010 * @param array $field
2014 public static function getDatePickerExtra($field) {
2016 if (isset($field['date_format'])) {
2017 $extra['date'] = $field['date_format'];
2018 $extra['time'] = $field['time_format'];
2020 $thisYear = date('Y');
2021 if (isset($field['start_date_years'])) {
2022 $extra['minDate'] = date('Y-m-d', strtotime('-' . ($thisYear - $field['start_date_years']) . ' years'));
2024 if (isset($field['end_date_years'])) {
2025 $extra['maxDate'] = date('Y-m-d', strtotime('-' . ($thisYear - $field['end_date_years']) . ' years'));
2031 * Get the attributes parameters required for datepicker.
2033 * @param array $field
2037 * Array ready to pass to $this->addForm('datepicker' as attributes.
2039 public static function getDatePickerAttributes(&$field) {
2042 'start_date_years' => 'minYear',
2043 'end_date_years' => 'maxYear',
2044 'date_format' => 'format',
2046 foreach ($dateAttributes as $dateAttribute => $mapTo) {
2047 if (isset($field[$dateAttribute])) {
2048 $attributes[$mapTo] = $field[$dateAttribute];
2055 * Function to convert mysql to date plugin format.
2057 * @param string $mysqlDate
2060 * @param null $formatType
2061 * @param null $format
2062 * @param null $timeFormat
2067 public static function setDateDefaults($mysqlDate = NULL, $formatType = NULL, $format = NULL, $timeFormat = NULL) {
2068 // if date is not passed assume it as today
2070 $mysqlDate = date('Y-m-d G:i:s');
2073 $config = CRM_Core_Config
::singleton();
2075 // get actual format
2076 $params = ['name' => $formatType];
2078 CRM_Core_DAO
::commonRetrieve('CRM_Core_DAO_PreferencesDate', $params, $values);
2080 if ($values['date_format']) {
2081 $format = $values['date_format'];
2084 if (isset($values['time_format'])) {
2085 $timeFormat = $values['time_format'];
2089 $dateFormat = 'm/d/Y';
2090 $date = date($dateFormat, strtotime($mysqlDate));
2093 $timeFormat = $config->timeInputFormat
;
2096 $actualTimeFormat = "g:iA";
2097 $appendZeroLength = 7;
2098 if ($timeFormat > 1) {
2099 $actualTimeFormat = "G:i";
2100 $appendZeroLength = 5;
2103 $time = date($actualTimeFormat, strtotime($mysqlDate));
2105 // need to append zero for hours < 10
2106 if (strlen($time) < $appendZeroLength) {
2107 $time = '0' . $time;
2110 return [$date, $time];
2114 * Function get date format.
2116 * @param string $formatType
2117 * Date name e.g. birth.
2121 public static function getDateFormat($formatType = NULL) {
2124 $format = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_PreferencesDate',
2125 $formatType, 'date_format', 'name'
2130 $config = CRM_Core_Config
::singleton();
2131 $format = $config->dateInputFormat
;
2137 * Date formatting for imports where date format is specified.
2139 * Note this is used for imports (only) because the importer can
2140 * specify the format.
2142 * Tests are in CRM_Utils_DateTest::testFormatDate
2145 * Date string as entered.
2147 * One of the constants like CRM_Core_Form_Date::DATE_yyyy_mm_dd.
2149 * @return null|string
2151 public static function formatDate($date, $dateType) {
2156 // 1. first convert date to default format.
2157 // 2. append time to default formatted date (might be removed during format)
2158 // 3. validate date / date time.
2159 // 4. If date and time then convert to default date time format.
2162 $dateParams = [$dateKey => $date];
2164 if (CRM_Utils_Date
::convertToDefaultDate($dateParams, $dateType, $dateKey)) {
2165 $dateVal = $dateParams[$dateKey];
2166 if ($dateType == 1) {
2168 // The seconds part of this regex is not quite right - but it does succeed
2169 // in clarifying whether there is a time component or not - which is all it is meant
2171 if (preg_match('/(\s(([01]\d)|[2][0-3]):([0-5]\d):?[0-5]?\d?)$/', $date, $matches)) {
2172 if (strpos($date, '-') !== FALSE) {
2173 $dateVal .= array_shift($matches);
2175 if (!CRM_Utils_Rule
::dateTime($dateVal)) {
2178 $dateVal = CRM_Utils_Date
::customFormat(preg_replace("/(:|\s)?/", '', $dateVal), '%Y%m%d%H%i%s');
2184 return CRM_Utils_Rule
::date($dateVal) ?
$dateVal : NULL;
2191 * Function to return days of the month.
2195 public static function getCalendarDayOfMonth() {
2197 for ($i = 1; $i <= 31; $i++
) {
2200 $month[$i] = $i . ' / Last day of month';
2207 * Convert a relative date format to an api field.
2209 * @param array $params
2210 * @param string $dateField
2211 * @param bool $isDatePicker
2212 * Non datepicker fields are deprecated. Exterminate Exterminate.
2213 * (but for now handle them).
2215 public static function convertFormDateToApiFormat(&$params, $dateField, $isDatePicker = TRUE) {
2216 if (!empty($params[$dateField . '_relative'])) {
2217 $dates = CRM_Utils_Date
::getFromTo($params[$dateField . '_relative'], NULL, NULL);
2218 unset($params[$dateField . '_relative']);
2220 if (!empty($params[$dateField . '_low'])) {
2221 $dates[0] = $isDatePicker ?
$params[$dateField . '_low'] : date('Y-m-d H:i:s', strtotime($params[$dateField . '_low']));
2222 unset($params[$dateField . '_low']);
2224 if (!empty($params[$dateField . '_high'])) {
2225 $dates[1] = $isDatePicker ?
$params[$dateField . '_high'] : date('Y-m-d 23:59:59', strtotime($params[$dateField . '_high']));
2226 unset($params[$dateField . '_high']);
2228 if (empty($dates)) {
2231 if (empty($dates[0])) {
2232 $params[$dateField] = ['<=' => $dates[1]];
2234 elseif (empty($dates[1])) {
2235 $params[$dateField] = ['>=' => $dates[0]];
2238 $params[$dateField] = ['BETWEEN' => $dates];
2243 * Print out a date object in specified format in local timezone
2245 * @param DateTimeObject $dateObject
2246 * @param string $format
2249 public static function convertDateToLocalTime($dateObject, $format = 'YmdHis') {
2250 $systemTimeZone = new DateTimeZone(CRM_Core_Config
::singleton()->userSystem
->getTimeZoneString());
2251 $dateObject->setTimezone($systemTimeZone);
2252 return $dateObject->format($format);
2256 * Check if the value returned by a date picker has a date section (ie: includes
2257 * a '-' character) if it includes a time section (ie: includes a ':').
2259 * @param string $value
2260 * A date/time string input from a datepicker value.
2263 * TRUE if valid, FALSE if there is a time without a date.
2265 public static function datePickerValueWithTimeHasDate($value) {
2266 // If there's no : (time) or a : and a - (date) then return true
2268 strpos($value, ':') === FALSE
2269 ||
strpos($value, ':') !== FALSE && strpos($value, '-') !== FALSE
2274 * Validate start and end dates entered on a form to make sure they are
2275 * logical. Expects the form keys to be start_date and end_date.
2277 * @param string $startFormKey
2278 * The form element key of the 'start date'
2279 * @param string $startValue
2280 * The value of the 'start date'
2281 * @param string $endFormKey
2282 * The form element key of the 'end date'
2283 * @param string $endValue
2284 * The value of the 'end date'
2286 * @return array|bool
2287 * TRUE if valid, an array of the erroneous form key, and error message to
2290 public static function validateStartEndDatepickerInputs($startFormKey, $startValue, $endFormKey, $endValue) {
2292 // Check date as well as time is set
2293 if (!empty($startValue) && !self
::datePickerValueWithTimeHasDate($startValue)) {
2294 return ['key' => $startFormKey, 'message' => ts('Please enter a date as well as a time.')];
2296 if (!empty($endValue) && !self
::datePickerValueWithTimeHasDate($endValue)) {
2297 return ['key' => $endFormKey, 'message' => ts('Please enter a date as well as a time.')];
2300 // Check end date is after start date
2301 if (!empty($startValue) && !empty($endValue) && $endValue < $startValue) {
2302 return ['key' => $endFormKey, 'message' => ts('The end date should be after the start date.')];