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 * @param null $timeStamp
701 * @return bool|string
703 public static function currentDBDate($timeStamp = NULL) {
704 return $timeStamp ?
date('YmdHis', $timeStamp) : date('YmdHis');
713 public static function overdue($date, $now = NULL) {
714 $mysqlDate = self
::isoToMysql($date);
716 $now = self
::currentDBDate();
719 $now = self
::isoToMysql($now);
722 return ($mysqlDate >= $now) ?
FALSE : TRUE;
726 * Get customized today.
728 * This function is used for getting customized today. To get
729 * actuall today pass 'dayParams' as null. or else pass the day,
730 * month, year values as array values
731 * Example: $dayParams = array(
732 * 'day' => '25', 'month' => '10',
733 * 'year' => '2007' );
735 * @param array $dayParams of the day, month, year.
736 * Array of the day, month, year.
738 * @param string $format
739 * Expected date format( default.
740 * format is 2007-12-21 )
743 * Return the customized today's date (Y-m-d)
745 public static function getToday($dayParams = NULL, $format = "Y-m-d") {
746 if (is_null($dayParams) ||
empty($dayParams)) {
747 $today = date($format);
750 $today = date($format, mktime(0, 0, 0,
761 * Find whether today's date lies in
764 * @param date $startDate
765 * Start date for the range.
766 * @param date $endDate
767 * End date for the range.
770 * true if today's date is in the given date range
772 public static function getRange($startDate, $endDate) {
773 $today = date("Y-m-d");
774 $mysqlStartDate = self
::isoToMysql($startDate);
775 $mysqlEndDate = self
::isoToMysql($endDate);
776 $mysqlToday = self
::isoToMysql($today);
778 if ((isset($mysqlStartDate) && isset($mysqlEndDate)) && (($mysqlToday >= $mysqlStartDate) && ($mysqlToday <= $mysqlEndDate))) {
781 elseif ((isset($mysqlStartDate) && !isset($mysqlEndDate)) && (($mysqlToday >= $mysqlStartDate))) {
784 elseif ((!isset($mysqlStartDate) && isset($mysqlEndDate)) && (($mysqlToday <= $mysqlEndDate))) {
791 * Get start date and end from
792 * the given relative term and unit
794 * @param string $relative Relative format in the format term.unit.
797 * @param string $from
799 * @param string $fromTime
800 * @param string $toTime
803 * start date, end date
805 public static function getFromTo($relative, $from, $to, $fromTime = NULL, $toTime = '235959') {
807 list($term, $unit) = explode('.', $relative, 2);
808 $dateRange = self
::relativeToAbsolute($term, $unit);
809 $from = substr($dateRange['from'], 0, 8);
810 $to = substr($dateRange['to'], 0, 8);
811 // @todo fix relativeToAbsolute & add tests
812 // relativeToAbsolute returns 8 char date strings
813 // or 14 char date + time strings.
814 // We should use those. However, it turns out to be unreliable.
815 // e.g. this.week does NOT return 235959 for 'from'
816 // so our defaults are more reliable.
817 // Currently relativeToAbsolute only supports 'whole' days so that is ok
820 $from = self
::processDate($from, $fromTime);
821 $to = self
::processDate($to, $toTime);
823 return array($from, $to);
827 * Calculate Age in Years if greater than one year else in months.
829 * @param date $birthDate
833 * array $results contains years or months
835 static public function calculateAge($birthDate) {
837 $formatedBirthDate = CRM_Utils_Date
::customFormat($birthDate, '%Y-%m-%d');
839 $bDate = explode('-', $formatedBirthDate);
840 $birthYear = $bDate[0];
841 $birthMonth = $bDate[1];
842 $birthDay = $bDate[2];
843 $year_diff = date("Y") - $birthYear;
845 // don't calculate age CRM-3143
846 if ($birthYear == '1902') {
850 switch ($year_diff) {
852 $month = (12 - $birthMonth) +
date("m");
854 if (date("d") < $birthDay) {
857 $results['months'] = $month;
859 elseif ($month == 12 && (date("d") < $birthDay)) {
860 $results['months'] = $month - 1;
863 $results['years'] = $year_diff;
868 $month = date("m") - $birthMonth;
869 $results['months'] = $month;
873 $results['years'] = $year_diff;
874 if ((date("m") < $birthMonth) ||
(date("m") == $birthMonth) && (date("d") < $birthDay)) {
883 * Calculate next payment date according to provided unit & interval
885 * @param string $unit
886 * Frequency unit like year,month, week etc.
888 * @param int $interval
889 * Frequency interval.
892 * Start date of pledge.
894 * @param bool $dontCareTime
897 * contains new date with added interval
899 public static function intervalAdd($unit, $interval, $date, $dontCareTime = FALSE) {
900 if (is_array($date)) {
901 $hour = CRM_Utils_Array
::value('H', $date);
902 $minute = CRM_Utils_Array
::value('i', $date);
903 $second = CRM_Utils_Array
::value('s', $date);
904 $month = CRM_Utils_Array
::value('M', $date);
905 $day = CRM_Utils_Array
::value('d', $date);
906 $year = CRM_Utils_Array
::value('Y', $date);
909 extract(date_parse($date));
911 $date = mktime($hour, $minute, $second, $month, $day, $year);
914 $date = mktime($hour, $minute, $second, $month, $day, $year +
$interval);
918 $date = mktime($hour, $minute, $second, $month +
$interval, $day, $year);
922 $interval = $interval * 7;
923 $date = mktime($hour, $minute, $second, $month, $day +
$interval, $year);
927 $date = mktime($hour, $minute, $second, $month, $day +
$interval, $year);
931 $date = mktime($hour, $minute, $second +
$interval, $month, $day, $year);
935 $scheduleDate = explode("-", date("n-j-Y-H-i-s", $date));
938 $date['M'] = $scheduleDate[0];
939 $date['d'] = $scheduleDate[1];
940 $date['Y'] = $scheduleDate[2];
941 if ($dontCareTime == FALSE) {
942 $date['H'] = $scheduleDate[3];
943 $date['i'] = $scheduleDate[4];
944 $date['s'] = $scheduleDate[5];
950 * Get the smarty view presentation mapping for the given format.
952 * Historically it was decided that where the view format is 'dd/mm/yy' or 'mm/dd/yy'
953 * they should be rendered using a longer date format. This is likely as much to
954 * do with the earlier date widget being unable to handle some formats as usablity.
955 * However, we continue to respect this.
958 * Given format ( eg 'M Y', 'Y M' ).
960 * @return string|null
961 * Smarty translation of the date format. Null is also valid and is translated
962 * according to the available parts at the smarty layer.
964 public static function getDateFieldViewFormat($format) {
965 $supportableFormats = array(
966 'mm/dd' => '%B %E%f',
967 'dd-mm' => '%E%f %B',
971 'dd/mm/yy' => '%E%f %B %Y',
974 return array_key_exists($format, $supportableFormats) ?
$supportableFormats[$format] : self
::pickBestSmartyFormat($format);
978 * Pick the smarty format from settings that best matches the time string we have.
980 * For view purposes we historically use the setting that most closely matches the data
981 * in the format from our settings, as opposed to the setting configured for the field.
986 public static function pickBestSmartyFormat($format) {
987 if (stristr($format, 'h')) {
988 return Civi
::settings()->get('dateformatDatetime');
990 if (stristr($format, 'd') ||
stristr($format, 'j')) {
991 return Civi
::settings()->get('dateformatFull');
993 if (stristr($format, 'm')) {
994 return Civi
::settings()->get('dateformatPartial');
996 return Civi
::settings()->get('dateformatYear');
1000 * Map date plugin and actual format that is used by PHP.
1004 public static function datePluginToPHPFormats() {
1005 $dateInputFormats = array(
1006 "mm/dd/yy" => 'm/d/Y',
1007 "dd/mm/yy" => 'd/m/Y',
1008 "yy-mm-dd" => 'Y-m-d',
1009 "dd-mm-yy" => 'd-m-Y',
1010 "dd.mm.yy" => 'd.m.Y',
1012 "M d, yy" => 'M j, Y',
1013 "d M yy" => 'j M Y',
1014 "MM d, yy" => 'F j, Y',
1015 "d MM yy" => 'j F Y',
1016 "DD, d MM yy" => 'l, j F Y',
1024 return $dateInputFormats;
1028 * Resolves the given relative time interval into finite time limits.
1030 * @param string $relativeTerm
1031 * Relative time frame: this, previous, previous_1.
1033 * Frequency unit like year, month, week etc.
1036 * start date and end date for the relative time frame
1038 public static function relativeToAbsolute($relativeTerm, $unit) {
1040 $from = $to = $dateRange = array();
1041 $from['H'] = $from['i'] = $from['s'] = 0;
1042 $relativeTermParts = explode('_', $relativeTerm);
1043 $relativeTermPrefix = $relativeTermParts[0];
1044 $relativeTermSuffix = isset($relativeTermParts[1]) ?
$relativeTermParts[1] : '';
1048 switch ($relativeTerm) {
1050 $from['d'] = $from['M'] = 1;
1053 $to['Y'] = $from['Y'] = $now['year'];
1057 $from['M'] = $from['d'] = 1;
1060 $to['Y'] = $from['Y'] = $now['year'] - 1;
1063 case 'previous_before':
1064 $from['M'] = $from['d'] = 1;
1067 $to['Y'] = $from['Y'] = $now['year'] - 2;
1071 $from['M'] = $from['d'] = 1;
1074 $from['Y'] = $now['year'] - 2;
1075 $to['Y'] = $now['year'] - 1;
1081 $to['Y'] = $now['year'] - 1;
1086 $from['M'] = $from['d'] = 1;
1087 $from['Y'] = $now['year'];
1091 case 'greater_previous':
1094 $from['Y'] = $now['year'] - 1;
1099 $to['d'] = $now['mday'];
1100 $to['M'] = $now['mon'];
1101 $to['Y'] = $now['year'];
1103 $to['i'] = $to['s'] = 59;
1104 $from = self
::intervalAdd('year', -1, $to);
1105 $from = self
::intervalAdd('second', 1, $from);
1109 $from['M'] = $from['d'] = 1;
1110 $from['Y'] = $now['year'];
1112 $to['i'] = $to['s'] = 59;
1113 $to['d'] = $now['mday'];
1114 $to['M'] = $now['mon'];
1115 $to['Y'] = $now['year'];
1119 $to['d'] = $now['mday'];
1120 $to['M'] = $now['mon'];
1121 $to['Y'] = $now['year'];
1123 $to['i'] = $to['s'] = 59;
1124 $from = self
::intervalAdd('year', -2, $to);
1125 $from = self
::intervalAdd('second', 1, $from);
1129 $to['d'] = $now['mday'];
1130 $to['M'] = $now['mon'];
1131 $to['Y'] = $now['year'];
1133 $to['i'] = $to['s'] = 59;
1134 $from = self
::intervalAdd('year', -3, $to);
1135 $from = self
::intervalAdd('second', 1, $from);
1141 $to['Y'] = $now['year'];
1146 $from['M'] = $from['d'] = 1;
1149 $to['Y'] = $from['Y'] = $now['year'] +
1;
1153 $from['d'] = $now['mday'];
1154 $from['M'] = $now['mon'];
1155 $from['Y'] = $now['year'];
1156 $to['d'] = $now['mday'] - 1;
1157 $to['M'] = $now['mon'];
1158 $to['Y'] = $now['year'] +
1;
1164 $config = CRM_Core_Config
::singleton();
1165 $from['d'] = $config->fiscalYearStart
['d'];
1166 $from['M'] = $config->fiscalYearStart
['M'];
1167 $fYear = self
::calculateFiscalYear($from['d'], $from['M']);
1168 switch ($relativeTermPrefix) {
1170 $from['Y'] = $fYear;
1171 $fiscalYear = mktime(0, 0, 0, $from['M'], $from['d'] - 1, $from['Y'] +
1);
1172 $fiscalEnd = explode('-', date("Y-m-d", $fiscalYear));
1174 $to['d'] = $fiscalEnd['2'];
1175 $to['M'] = $fiscalEnd['1'];
1176 $to['Y'] = $fiscalEnd['0'];
1180 if (!is_numeric($relativeTermSuffix)) {
1181 $from['Y'] = ($relativeTermSuffix === 'before') ?
$fYear - 2 : $fYear - 1;
1182 $fiscalYear = mktime(0, 0, 0, $from['M'], $from['d'] - 1, $from['Y'] +
1);
1183 $fiscalEnd = explode('-', date("Y-m-d", $fiscalYear));
1184 $to['d'] = $fiscalEnd['2'];
1185 $to['M'] = $fiscalEnd['1'];
1186 $to['Y'] = $fiscalEnd['0'];
1189 $from['Y'] = $fYear - $relativeTermSuffix;
1190 $fiscalYear = mktime(0, 0, 0, $from['M'], $from['d'] - 1, $from['Y'] +
1);
1191 $fiscalEnd = explode('-', date("Y-m-d", $fiscalYear));
1192 $to['d'] = $fiscalEnd['2'];
1193 $to['M'] = $fiscalEnd['1'];
1199 $from['Y'] = $fYear +
1;
1200 $fiscalYear = mktime(0, 0, 0, $from['M'], $from['d'] - 1, $from['Y'] +
1);
1201 $fiscalEnd = explode('-', date("Y-m-d", $fiscalYear));
1202 $to['d'] = $fiscalEnd['2'];
1203 $to['M'] = $fiscalEnd['1'];
1204 $to['Y'] = $fiscalEnd['0'];
1210 switch ($relativeTerm) {
1213 $quarter = ceil($now['mon'] / 3);
1215 $from['M'] = (3 * $quarter) - 2;
1216 $to['M'] = 3 * $quarter;
1217 $to['Y'] = $from['Y'] = $now['year'];
1218 $to['d'] = date('t', mktime(0, 0, 0, $to['M'], 1, $now['year']));
1223 $quarter = ceil($now['mon'] / 3);
1224 $quarter = $quarter - $difference;
1226 if ($quarter <= 0) {
1231 $from['M'] = (3 * $quarter) - 2;
1232 $to['M'] = 3 * $quarter;
1233 $to['Y'] = $from['Y'] = $now['year'] - $subtractYear;
1234 $to['d'] = date('t', mktime(0, 0, 0, $to['M'], 1, $to['Y']));
1237 case 'previous_before':
1239 $quarter = ceil($now['mon'] / 3);
1240 $quarter = $quarter - $difference;
1242 if ($quarter <= 0) {
1247 $from['M'] = (3 * $quarter) - 2;
1248 $to['M'] = 3 * $quarter;
1249 $to['Y'] = $from['Y'] = $now['year'] - $subtractYear;
1250 $to['d'] = date('t', mktime(0, 0, 0, $to['M'], 1, $to['Y']));
1255 $quarter = ceil($now['mon'] / 3);
1256 $current_quarter = $quarter;
1257 $quarter = $quarter - $difference;
1259 if ($quarter <= 0) {
1264 $from['M'] = (3 * $quarter) - 2;
1265 switch ($current_quarter) {
1267 $to['M'] = (4 * $quarter);
1271 $to['M'] = (4 * $quarter) +
3;
1275 $to['M'] = (4 * $quarter) +
2;
1279 $to['M'] = (4 * $quarter) +
1;
1282 $to['Y'] = $from['Y'] = $now['year'] - $subtractYear;
1283 if ($to['M'] > 12) {
1284 $to['M'] = 3 * ($quarter - 3);
1285 $to['Y'] = $now['year'];
1287 $to['d'] = date('t', mktime(0, 0, 0, $to['M'], 1, $to['Y']));
1291 $quarter = ceil($now['mon'] / 3) - 1;
1293 if ($quarter <= 0) {
1297 $to['M'] = 3 * $quarter;
1298 $to['Y'] = $from['Y'] = $now['year'] - $subtractYear;
1299 $to['d'] = date('t', mktime(0, 0, 0, $to['M'], 1, $to['Y']));
1304 $quarter = ceil($now['mon'] / 3);
1306 $from['M'] = (3 * $quarter) - 2;
1307 $from['Y'] = $now['year'];
1311 case 'greater_previous':
1312 $quarter = ceil($now['mon'] / 3) - 1;
1314 if ($quarter <= 0) {
1318 $from['M'] = 3 * $quarter;
1319 $from['Y'] = $from['Y'] = $now['year'] - $subtractYear;
1320 $from['d'] = date('t', mktime(0, 0, 0, $from['M'], 1, $from['Y']));
1325 $to['d'] = $now['mday'];
1326 $to['M'] = $now['mon'];
1327 $to['Y'] = $now['year'];
1329 $to['i'] = $to['s'] = 59;
1330 $from = self
::intervalAdd('day', -90, $to);
1331 $from = self
::intervalAdd('second', 1, $from);
1335 $quarter = ceil($now['mon'] / 3);
1337 $from['M'] = (3 * $quarter) - 2;
1338 $from['Y'] = $now['year'];
1339 $to['d'] = $now['mday'];
1340 $to['M'] = $now['mon'];
1341 $to['Y'] = $now['year'];
1343 $to['i'] = $to['s'] = 59;
1347 $quarter = ceil($now['mon'] / 3);
1348 $to['M'] = 3 * $quarter;
1349 $to['Y'] = $now['year'];
1350 $to['d'] = date('t', mktime(0, 0, 0, $to['M'], 1, $now['year']));
1357 $quarter = ceil($now['mon'] / 3);
1358 $quarter = $quarter - $difference;
1361 $now['year'] = $now['year'] +
1;
1364 if ($quarter <= 0) {
1369 $from['M'] = (3 * $quarter) - 2;
1370 $to['M'] = 3 * $quarter;
1371 $to['Y'] = $from['Y'] = $now['year'] - $subtractYear;
1372 $to['d'] = date('t', mktime(0, 0, 0, $to['M'], 1, $to['Y']));
1376 $from['d'] = $now['mday'];
1377 $from['M'] = $now['mon'];
1378 $from['Y'] = $now['year'];
1380 $from['i'] = $to['s'] = 00;
1381 $to = self
::intervalAdd('day', 90, $from);
1382 $to = self
::intervalAdd('second', -1, $to);
1388 switch ($relativeTerm) {
1391 $to['d'] = date('t', mktime(0, 0, 0, $now['mon'], 1, $now['year']));
1392 $from['M'] = $to['M'] = $now['mon'];
1393 $from['Y'] = $to['Y'] = $now['year'];
1398 if ($now['mon'] == 1) {
1399 $from['M'] = $to['M'] = 12;
1400 $from['Y'] = $to['Y'] = $now['year'] - 1;
1403 $from['M'] = $to['M'] = $now['mon'] - 1;
1404 $from['Y'] = $to['Y'] = $now['year'];
1406 $to['d'] = date('t', mktime(0, 0, 0, $to['M'], 1, $to['Y']));
1409 case 'previous_before':
1411 if ($now['mon'] < 3) {
1412 $from['M'] = $to['M'] = 10 +
$now['mon'];
1413 $from['Y'] = $to['Y'] = $now['year'] - 1;
1416 $from['M'] = $to['M'] = $now['mon'] - 2;
1417 $from['Y'] = $to['Y'] = $now['year'];
1419 $to['d'] = date('t', mktime(0, 0, 0, $to['M'], 1, $to['Y']));
1424 if ($now['mon'] < 3) {
1425 $from['M'] = 10 +
$now['mon'];
1426 $from['Y'] = $now['year'] - 1;
1429 $from['M'] = $now['mon'] - 2;
1430 $from['Y'] = $now['year'];
1433 if ($now['mon'] == 1) {
1435 $to['Y'] = $now['year'] - 1;
1438 $to['M'] = $now['mon'] - 1;
1439 $to['Y'] = $now['year'];
1442 $to['d'] = date('t', mktime(0, 0, 0, $to['M'], 1, $to['Y']));
1446 // before end of past month
1447 if ($now['mon'] == 1) {
1449 $to['Y'] = $now['year'] - 1;
1452 $to['M'] = $now['mon'] - 1;
1453 $to['Y'] = $now['year'];
1456 $to['d'] = date('t', mktime(0, 0, 0, $to['M'], 1, $to['Y']));
1462 $from['M'] = $now['mon'];;
1463 $from['Y'] = $now['year'];
1467 case 'greater_previous':
1468 // from end of past month
1469 if ($now['mon'] == 1) {
1471 $from['Y'] = $now['year'] - 1;
1474 $from['M'] = $now['mon'] - 1;
1475 $from['Y'] = $now['year'];
1478 $from['d'] = date('t', mktime(0, 0, 0, $from['M'], 1, $from['Y']));
1483 $to['d'] = $now['mday'];
1484 $to['M'] = $now['mon'];
1485 $to['Y'] = $now['year'];
1487 $to['i'] = $to['s'] = 59;
1488 $from = self
::intervalAdd('day', -60, $to);
1489 $from = self
::intervalAdd('second', 1, $from);
1493 $to['d'] = $now['mday'];
1494 $to['M'] = $now['mon'];
1495 $to['Y'] = $now['year'];
1497 $to['i'] = $to['s'] = 59;
1498 $from = self
::intervalAdd('day', -30, $to);
1499 $from = self
::intervalAdd('second', 1, $from);
1504 $from['M'] = $now['mon'];;
1505 $from['Y'] = $now['year'];
1506 $to['d'] = $now['mday'];
1507 $to['M'] = $now['mon'];
1508 $to['Y'] = $now['year'];
1510 $to['i'] = $to['s'] = 59;
1515 $to['Y'] = $now['year'];
1516 $to['M'] = $now['mon'];
1517 $to['d'] = date('t', mktime(0, 0, 0, $now['mon'], 1, $now['year']));
1523 if ($now['mon'] == 12) {
1524 $from['M'] = $to['M'] = 1;
1525 $from['Y'] = $to['Y'] = $now['year'] +
1;
1528 $from['M'] = $to['M'] = $now['mon'] +
1;
1529 $from['Y'] = $to['Y'] = $now['year'];
1531 $to['d'] = date('t', mktime(0, 0, 0, $to['M'], 1, $to['Y']));
1535 $from['d'] = $now['mday'];
1536 $from['M'] = $now['mon'];
1537 $from['Y'] = $now['year'];
1539 $from['i'] = $to['s'] = 00;
1540 $to = self
::intervalAdd('day', 30, $from);
1541 $to = self
::intervalAdd('second', -1, $to);
1545 $from['d'] = $now['mday'];
1546 $from['M'] = $now['mon'];
1547 $from['Y'] = $now['year'];
1549 $from['i'] = $to['s'] = 00;
1550 $to = self
::intervalAdd('day', 60, $from);
1551 $to = self
::intervalAdd('second', -1, $to);
1557 $weekFirst = Civi
::settings()->get('weekBegins');
1558 $thisDay = $now['wday'];
1559 if ($weekFirst > $thisDay) {
1560 $diffDay = $thisDay - $weekFirst +
7;
1563 $diffDay = $thisDay - $weekFirst;
1565 switch ($relativeTerm) {
1567 $from['d'] = $now['mday'];
1568 $from['M'] = $now['mon'];
1569 $from['Y'] = $now['year'];
1570 $from = self
::intervalAdd('day', -1 * ($diffDay), $from);
1571 $to = self
::intervalAdd('day', 6, $from);
1575 $from['d'] = $now['mday'];
1576 $from['M'] = $now['mon'];
1577 $from['Y'] = $now['year'];
1578 $from = self
::intervalAdd('day', -1 * ($diffDay) - 7, $from);
1579 $to = self
::intervalAdd('day', 6, $from);
1582 case 'previous_before':
1583 $from['d'] = $now['mday'];
1584 $from['M'] = $now['mon'];
1585 $from['Y'] = $now['year'];
1586 $from = self
::intervalAdd('day', -1 * ($diffDay) - 14, $from);
1587 $to = self
::intervalAdd('day', 6, $from);
1591 $from['d'] = $now['mday'];
1592 $from['M'] = $now['mon'];
1593 $from['Y'] = $now['year'];
1594 $from = self
::intervalAdd('day', -1 * ($diffDay) - 14, $from);
1595 $to = self
::intervalAdd('day', 13, $from);
1599 $to['d'] = $now['mday'];
1600 $to['M'] = $now['mon'];
1601 $to['Y'] = $now['year'];
1602 $to = self
::intervalAdd('day', -1 * ($diffDay) - 1, $to);
1607 $from['d'] = $now['mday'];
1608 $from['M'] = $now['mon'];
1609 $from['Y'] = $now['year'];
1610 $from = self
::intervalAdd('day', -1 * ($diffDay), $from);
1614 case 'greater_previous':
1615 $from['d'] = $now['mday'];
1616 $from['M'] = $now['mon'];
1617 $from['Y'] = $now['year'];
1618 $from = self
::intervalAdd('day', -1 * ($diffDay) - 1, $from);
1623 $to['d'] = $now['mday'];
1624 $to['M'] = $now['mon'];
1625 $to['Y'] = $now['year'];
1627 $to['i'] = $to['s'] = 59;
1628 $from = self
::intervalAdd('day', -7, $to);
1629 $from = self
::intervalAdd('second', 1, $from);
1633 $from['d'] = $now['mday'];
1634 $from['M'] = $now['mon'];
1635 $from['Y'] = $now['year'];
1636 $from = self
::intervalAdd('day', -1 * ($diffDay), $from);
1637 $to['d'] = $now['mday'];
1638 $to['M'] = $now['mon'];
1639 $to['Y'] = $now['year'];
1641 $to['i'] = $to['s'] = 59;
1645 $to['d'] = $now['mday'];
1646 $to['M'] = $now['mon'];
1647 $to['Y'] = $now['year'];
1649 $to = self
::intervalAdd('day', -1 * ($diffDay) +
6, $to);
1654 $from['d'] = $now['mday'];
1655 $from['M'] = $now['mon'];
1656 $from['Y'] = $now['year'];
1657 $from = self
::intervalAdd('day', -1 * ($diffDay) +
7, $from);
1658 $to = self
::intervalAdd('day', 6, $from);
1662 $from['d'] = $now['mday'];
1663 $from['M'] = $now['mon'];
1664 $from['Y'] = $now['year'];
1666 $from['i'] = $to['s'] = 00;
1667 $to = self
::intervalAdd('day', 7, $from);
1668 $to = self
::intervalAdd('second', -1, $to);
1674 switch ($relativeTerm) {
1676 $from['d'] = $to['d'] = $now['mday'];
1677 $from['M'] = $to['M'] = $now['mon'];
1678 $from['Y'] = $to['Y'] = $now['year'];
1682 $from['d'] = $now['mday'];
1683 $from['M'] = $now['mon'];
1684 $from['Y'] = $now['year'];
1685 $from = self
::intervalAdd('day', -1, $from);
1686 $to['d'] = $from['d'];
1687 $to['M'] = $from['M'];
1688 $to['Y'] = $from['Y'];
1691 case 'previous_before':
1692 $from['d'] = $now['mday'];
1693 $from['M'] = $now['mon'];
1694 $from['Y'] = $now['year'];
1695 $from = self
::intervalAdd('day', -2, $from);
1696 $to['d'] = $from['d'];
1697 $to['M'] = $from['M'];
1698 $to['Y'] = $from['Y'];
1702 $from['d'] = $to['d'] = $now['mday'];
1703 $from['M'] = $to['M'] = $now['mon'];
1704 $from['Y'] = $to['Y'] = $now['year'];
1705 $from = self
::intervalAdd('day', -2, $from);
1706 $to = self
::intervalAdd('day', -1, $to);
1710 $to['d'] = $now['mday'];
1711 $to['M'] = $now['mon'];
1712 $to['Y'] = $now['year'];
1717 $from['d'] = $now['mday'];
1718 $from['M'] = $now['mon'];;
1719 $from['Y'] = $now['year'];
1724 $to['d'] = $now['mday'];
1725 $to['M'] = $now['mon'];
1726 $to['Y'] = $now['year'];
1727 $to = self
::intervalAdd('day', 1, $to);
1728 $from['d'] = $to['d'];
1729 $from['M'] = $to['M'];
1730 $from['Y'] = $to['Y'];
1741 if (!empty($
$item)) {
1742 $dateRange[$item] = self
::format($
$item);
1745 $dateRange[$item] = NULL;
1752 * Calculate current fiscal year based on the fiscal month and day.
1754 * @param int $fyDate
1755 * Fiscal start date.
1757 * @param int $fyMonth
1758 * Fiscal Start Month.
1761 * $fy Current Fiscal Year
1763 public static function calculateFiscalYear($fyDate, $fyMonth) {
1764 $date = date("Y-m-d");
1765 $currentYear = date("Y");
1767 // recalculate the date because month 4::04 make the difference
1768 $fiscalYear = explode('-', date("Y-m-d", mktime(0, 0, 0, $fyMonth, $fyDate, $currentYear)));
1769 $fyDate = $fiscalYear[2];
1770 $fyMonth = $fiscalYear[1];
1771 $fyStartDate = date("Y-m-d", mktime(0, 0, 0, $fyMonth, $fyDate, $currentYear));
1773 if ($fyStartDate > $date) {
1774 $fy = intval(intval($currentYear) - 1);
1777 $fy = intval($currentYear);
1783 * Function to process date, convert to mysql format
1785 * @param string $date
1787 * @param string $time
1789 * @param bool|string $returnNullString 'null' needs to be returned
1790 * so that db oject will set null in db
1791 * @param string $format
1792 * Expected return date format.( default is mysql ).
1795 * date format that is excepted by mysql
1797 public static function processDate($date, $time = NULL, $returnNullString = FALSE, $format = 'YmdHis') {
1800 if ($returnNullString) {
1801 $mysqlDate = 'null';
1805 $mysqlDate = date($format, strtotime($date . ' ' . $time));
1812 * Add the metadata about a date field to the field.
1814 * This metadata will work with the call $form->add('datepicker', ...
1816 * @param array $fieldMetaData
1817 * @param array $field
1821 public static function addDateMetadataToField($fieldMetaData, $field) {
1822 if (isset($fieldMetaData['html'])) {
1823 $field['html_type'] = $fieldMetaData['html']['type'];
1824 if ($field['html_type'] === 'Select Date') {
1825 if (!isset($field['date_format'])) {
1826 $dateAttributes = CRM_Core_SelectValues
::date($fieldMetaData['html']['formatType'], NULL, NULL, NULL, 'Input');
1827 $field['start_date_years'] = $dateAttributes['minYear'];
1828 $field['end_date_years'] = $dateAttributes['maxYear'];
1829 $field['date_format'] = $dateAttributes['format'];
1830 $field['is_datetime_field'] = TRUE;
1831 $field['time_format'] = $dateAttributes['time'];
1832 $field['smarty_view_format'] = $dateAttributes['smarty_view_format'];
1834 $field['datepicker']['extra'] = self
::getDatePickerExtra($field);
1835 $field['datepicker']['attributes'] = self
::getDatePickerAttributes($field);
1843 * Get the fields required for the 'extra' parameter when adding a datepicker.
1845 * @param array $field
1849 public static function getDatePickerExtra($field) {
1851 if (isset($field['date_format'])) {
1852 $extra['date'] = $field['date_format'];
1853 $extra['time'] = $field['time_format'];
1855 $thisYear = date('Y');
1856 if (isset($field['start_date_years'])) {
1857 $extra['minDate'] = date('Y-m-d', strtotime('-' . ($thisYear - $field['start_date_years']) . ' years'));
1859 if (isset($field['end_date_years'])) {
1860 $extra['maxDate'] = date('Y-m-d', strtotime('-' . ($thisYear - $field['end_date_years']) . ' years'));
1866 * Get the attributes parameters required for datepicker.
1868 * @param array $field
1872 * Array ready to pass to $this->addForm('datepicker' as attributes.
1874 public static function getDatePickerAttributes(&$field) {
1875 $attributes = array();
1876 $dateAttributes = array(
1877 'start_date_years' => 'minYear',
1878 'end_date_years' => 'maxYear',
1879 'date_format' => 'format',
1881 foreach ($dateAttributes as $dateAttribute => $mapTo) {
1882 if (isset($field[$dateAttribute])) {
1883 $attributes[$mapTo] = $field[$dateAttribute];
1890 * Function to convert mysql to date plugin format.
1892 * @param string $mysqlDate
1895 * @param null $formatType
1896 * @param null $format
1897 * @param null $timeFormat
1902 public static function setDateDefaults($mysqlDate = NULL, $formatType = NULL, $format = NULL, $timeFormat = NULL) {
1903 // if date is not passed assume it as today
1905 $mysqlDate = date('Y-m-d G:i:s');
1908 $config = CRM_Core_Config
::singleton();
1910 // get actual format
1911 $params = array('name' => $formatType);
1913 CRM_Core_DAO
::commonRetrieve('CRM_Core_DAO_PreferencesDate', $params, $values);
1915 if ($values['date_format']) {
1916 $format = $values['date_format'];
1919 if (isset($values['time_format'])) {
1920 $timeFormat = $values['time_format'];
1924 // now we set display date using js, hence we should always setdefault
1925 // 'm/d/Y' format. So that submitted value is alwats mm/dd/YY format
1926 // note that for date display we dynamically create text field
1929 $format = $config->dateInputFormat;
1932 // get actual format
1933 $actualPHPFormats = CRM_Core_SelectValues::datePluginToPHPFormats( );
1934 $dateFormat = CRM_Utils_Array::value( $format, $actualPHPFormats );
1937 $dateFormat = 'm/d/Y';
1938 $date = date($dateFormat, strtotime($mysqlDate));
1941 $timeFormat = $config->timeInputFormat
;
1944 $actualTimeFormat = "g:iA";
1945 $appendZeroLength = 7;
1946 if ($timeFormat > 1) {
1947 $actualTimeFormat = "G:i";
1948 $appendZeroLength = 5;
1951 $time = date($actualTimeFormat, strtotime($mysqlDate));
1953 // need to append zero for hours < 10
1954 if (strlen($time) < $appendZeroLength) {
1955 $time = '0' . $time;
1958 return array($date, $time);
1962 * Function get date format.
1964 * @param string $formatType
1965 * Date name e.g. birth.
1969 public static function getDateFormat($formatType = NULL) {
1972 $format = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_PreferencesDate',
1973 $formatType, 'date_format', 'name'
1978 $config = CRM_Core_Config
::singleton();
1979 $format = $config->dateInputFormat
;
1988 * @return null|string
1990 public static function formatDate($date, $dateType) {
1991 $formattedDate = NULL;
1993 return $formattedDate;
1996 // 1. first convert date to default format.
1997 // 2. append time to default formatted date (might be removed during format)
1998 // 3. validate date / date time.
1999 // 4. If date and time then convert to default date time format.
2002 $dateParams = array($dateKey => $date);
2004 if (CRM_Utils_Date
::convertToDefaultDate($dateParams, $dateType, $dateKey)) {
2005 $dateVal = $dateParams[$dateKey];
2007 if ($dateType == 1) {
2009 if (preg_match("/(\s(([01]\d)|[2][0-3]):([0-5]\d))$/", $date, $matches)) {
2010 $ruleName = 'dateTime';
2011 if (strpos($date, '-') !== FALSE) {
2012 $dateVal .= array_shift($matches);
2018 $valid = CRM_Utils_Rule
::$ruleName($dateVal);
2021 // format date and time to default.
2022 if ($ruleName == 'dateTime') {
2023 $dateVal = CRM_Utils_Date
::customFormat(preg_replace("/(:|\s)?/", "", $dateVal), '%Y%m%d%H%i');
2024 // hack to add seconds
2027 $formattedDate = $dateVal;
2031 return $formattedDate;
2035 * Function to return days of the month.
2039 public static function getCalendarDayOfMonth() {
2041 for ($i = 1; $i <= 31; $i++
) {
2044 $month[$i] = $i . ' / Last day of month';