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 array $relativeTerm
1031 * Relative time frame like this, previous, etc.
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;
1045 switch ($relativeTerm) {
1047 $from['d'] = $from['M'] = 1;
1050 $to['Y'] = $from['Y'] = $now['year'];
1054 $from['M'] = $from['d'] = 1;
1057 $to['Y'] = $from['Y'] = $now['year'] - 1;
1060 case 'previous_before':
1061 $from['M'] = $from['d'] = 1;
1064 $to['Y'] = $from['Y'] = $now['year'] - 2;
1068 $from['M'] = $from['d'] = 1;
1071 $from['Y'] = $now['year'] - 2;
1072 $to['Y'] = $now['year'] - 1;
1078 $to['Y'] = $now['year'] - 1;
1083 $from['M'] = $from['d'] = 1;
1084 $from['Y'] = $now['year'];
1088 case 'greater_previous':
1091 $from['Y'] = $now['year'] - 1;
1096 $to['d'] = $now['mday'];
1097 $to['M'] = $now['mon'];
1098 $to['Y'] = $now['year'];
1100 $to['i'] = $to['s'] = 59;
1101 $from = self
::intervalAdd('year', -1, $to);
1102 $from = self
::intervalAdd('second', 1, $from);
1106 $from['M'] = $from['d'] = 1;
1107 $from['Y'] = $now['year'];
1109 $to['i'] = $to['s'] = 59;
1110 $to['d'] = $now['mday'];
1111 $to['M'] = $now['mon'];
1112 $to['Y'] = $now['year'];
1116 $to['d'] = $now['mday'];
1117 $to['M'] = $now['mon'];
1118 $to['Y'] = $now['year'];
1120 $to['i'] = $to['s'] = 59;
1121 $from = self
::intervalAdd('year', -2, $to);
1122 $from = self
::intervalAdd('second', 1, $from);
1126 $to['d'] = $now['mday'];
1127 $to['M'] = $now['mon'];
1128 $to['Y'] = $now['year'];
1130 $to['i'] = $to['s'] = 59;
1131 $from = self
::intervalAdd('year', -3, $to);
1132 $from = self
::intervalAdd('second', 1, $from);
1138 $to['Y'] = $now['year'];
1143 $from['M'] = $from['d'] = 1;
1146 $to['Y'] = $from['Y'] = $now['year'] +
1;
1150 $from['d'] = $now['mday'];
1151 $from['M'] = $now['mon'];
1152 $from['Y'] = $now['year'];
1153 $to['d'] = $now['mday'] - 1;
1154 $to['M'] = $now['mon'];
1155 $to['Y'] = $now['year'] +
1;
1161 $config = CRM_Core_Config
::singleton();
1162 $from['d'] = $config->fiscalYearStart
['d'];
1163 $from['M'] = $config->fiscalYearStart
['M'];
1164 $fYear = self
::calculateFiscalYear($from['d'], $from['M']);
1165 switch ($relativeTerm) {
1167 $from['Y'] = $fYear;
1168 $fiscalYear = mktime(0, 0, 0, $from['M'], $from['d'] - 1, $from['Y'] +
1);
1169 $fiscalEnd = explode('-', date("Y-m-d", $fiscalYear));
1171 $to['d'] = $fiscalEnd['2'];
1172 $to['M'] = $fiscalEnd['1'];
1173 $to['Y'] = $fiscalEnd['0'];
1177 $from['Y'] = $fYear - 1;
1178 $fiscalYear = mktime(0, 0, 0, $from['M'], $from['d'] - 1, $from['Y'] +
1);
1179 $fiscalEnd = explode('-', date("Y-m-d", $fiscalYear));
1180 $to['d'] = $fiscalEnd['2'];
1181 $to['M'] = $fiscalEnd['1'];
1182 $to['Y'] = $fiscalEnd['0'];
1186 $from['Y'] = $fYear +
1;
1187 $fiscalYear = mktime(0, 0, 0, $from['M'], $from['d'] - 1, $from['Y'] +
1);
1188 $fiscalEnd = explode('-', date("Y-m-d", $fiscalYear));
1189 $to['d'] = $fiscalEnd['2'];
1190 $to['M'] = $fiscalEnd['1'];
1191 $to['Y'] = $fiscalEnd['0'];
1197 switch ($relativeTerm) {
1200 $quarter = ceil($now['mon'] / 3);
1202 $from['M'] = (3 * $quarter) - 2;
1203 $to['M'] = 3 * $quarter;
1204 $to['Y'] = $from['Y'] = $now['year'];
1205 $to['d'] = date('t', mktime(0, 0, 0, $to['M'], 1, $now['year']));
1210 $quarter = ceil($now['mon'] / 3);
1211 $quarter = $quarter - $difference;
1213 if ($quarter <= 0) {
1218 $from['M'] = (3 * $quarter) - 2;
1219 $to['M'] = 3 * $quarter;
1220 $to['Y'] = $from['Y'] = $now['year'] - $subtractYear;
1221 $to['d'] = date('t', mktime(0, 0, 0, $to['M'], 1, $to['Y']));
1224 case 'previous_before':
1226 $quarter = ceil($now['mon'] / 3);
1227 $quarter = $quarter - $difference;
1229 if ($quarter <= 0) {
1234 $from['M'] = (3 * $quarter) - 2;
1235 $to['M'] = 3 * $quarter;
1236 $to['Y'] = $from['Y'] = $now['year'] - $subtractYear;
1237 $to['d'] = date('t', mktime(0, 0, 0, $to['M'], 1, $to['Y']));
1242 $quarter = ceil($now['mon'] / 3);
1243 $current_quarter = $quarter;
1244 $quarter = $quarter - $difference;
1246 if ($quarter <= 0) {
1251 $from['M'] = (3 * $quarter) - 2;
1252 switch ($current_quarter) {
1254 $to['M'] = (4 * $quarter);
1258 $to['M'] = (4 * $quarter) +
3;
1262 $to['M'] = (4 * $quarter) +
2;
1266 $to['M'] = (4 * $quarter) +
1;
1269 $to['Y'] = $from['Y'] = $now['year'] - $subtractYear;
1270 if ($to['M'] > 12) {
1271 $to['M'] = 3 * ($quarter - 3);
1272 $to['Y'] = $now['year'];
1274 $to['d'] = date('t', mktime(0, 0, 0, $to['M'], 1, $to['Y']));
1278 $quarter = ceil($now['mon'] / 3) - 1;
1280 if ($quarter <= 0) {
1284 $to['M'] = 3 * $quarter;
1285 $to['Y'] = $from['Y'] = $now['year'] - $subtractYear;
1286 $to['d'] = date('t', mktime(0, 0, 0, $to['M'], 1, $to['Y']));
1291 $quarter = ceil($now['mon'] / 3);
1293 $from['M'] = (3 * $quarter) - 2;
1294 $from['Y'] = $now['year'];
1298 case 'greater_previous':
1299 $quarter = ceil($now['mon'] / 3) - 1;
1301 if ($quarter <= 0) {
1305 $from['M'] = 3 * $quarter;
1306 $from['Y'] = $from['Y'] = $now['year'] - $subtractYear;
1307 $from['d'] = date('t', mktime(0, 0, 0, $from['M'], 1, $from['Y']));
1312 $to['d'] = $now['mday'];
1313 $to['M'] = $now['mon'];
1314 $to['Y'] = $now['year'];
1316 $to['i'] = $to['s'] = 59;
1317 $from = self
::intervalAdd('day', -90, $to);
1318 $from = self
::intervalAdd('second', 1, $from);
1322 $quarter = ceil($now['mon'] / 3);
1324 $from['M'] = (3 * $quarter) - 2;
1325 $from['Y'] = $now['year'];
1326 $to['d'] = $now['mday'];
1327 $to['M'] = $now['mon'];
1328 $to['Y'] = $now['year'];
1330 $to['i'] = $to['s'] = 59;
1334 $quarter = ceil($now['mon'] / 3);
1335 $to['M'] = 3 * $quarter;
1336 $to['Y'] = $now['year'];
1337 $to['d'] = date('t', mktime(0, 0, 0, $to['M'], 1, $now['year']));
1344 $quarter = ceil($now['mon'] / 3);
1345 $quarter = $quarter - $difference;
1348 $now['year'] = $now['year'] +
1;
1351 if ($quarter <= 0) {
1356 $from['M'] = (3 * $quarter) - 2;
1357 $to['M'] = 3 * $quarter;
1358 $to['Y'] = $from['Y'] = $now['year'] - $subtractYear;
1359 $to['d'] = date('t', mktime(0, 0, 0, $to['M'], 1, $to['Y']));
1363 $from['d'] = $now['mday'];
1364 $from['M'] = $now['mon'];
1365 $from['Y'] = $now['year'];
1367 $from['i'] = $to['s'] = 00;
1368 $to = self
::intervalAdd('day', 90, $from);
1369 $to = self
::intervalAdd('second', -1, $to);
1375 switch ($relativeTerm) {
1378 $to['d'] = date('t', mktime(0, 0, 0, $now['mon'], 1, $now['year']));
1379 $from['M'] = $to['M'] = $now['mon'];
1380 $from['Y'] = $to['Y'] = $now['year'];
1385 if ($now['mon'] == 1) {
1386 $from['M'] = $to['M'] = 12;
1387 $from['Y'] = $to['Y'] = $now['year'] - 1;
1390 $from['M'] = $to['M'] = $now['mon'] - 1;
1391 $from['Y'] = $to['Y'] = $now['year'];
1393 $to['d'] = date('t', mktime(0, 0, 0, $to['M'], 1, $to['Y']));
1396 case 'previous_before':
1398 if ($now['mon'] < 3) {
1399 $from['M'] = $to['M'] = 10 +
$now['mon'];
1400 $from['Y'] = $to['Y'] = $now['year'] - 1;
1403 $from['M'] = $to['M'] = $now['mon'] - 2;
1404 $from['Y'] = $to['Y'] = $now['year'];
1406 $to['d'] = date('t', mktime(0, 0, 0, $to['M'], 1, $to['Y']));
1411 if ($now['mon'] < 3) {
1412 $from['M'] = 10 +
$now['mon'];
1413 $from['Y'] = $now['year'] - 1;
1416 $from['M'] = $now['mon'] - 2;
1417 $from['Y'] = $now['year'];
1420 if ($now['mon'] == 1) {
1422 $to['Y'] = $now['year'] - 1;
1425 $to['M'] = $now['mon'] - 1;
1426 $to['Y'] = $now['year'];
1429 $to['d'] = date('t', mktime(0, 0, 0, $to['M'], 1, $to['Y']));
1433 // before end of past month
1434 if ($now['mon'] == 1) {
1436 $to['Y'] = $now['year'] - 1;
1439 $to['M'] = $now['mon'] - 1;
1440 $to['Y'] = $now['year'];
1443 $to['d'] = date('t', mktime(0, 0, 0, $to['M'], 1, $to['Y']));
1449 $from['M'] = $now['mon'];;
1450 $from['Y'] = $now['year'];
1454 case 'greater_previous':
1455 // from end of past month
1456 if ($now['mon'] == 1) {
1458 $from['Y'] = $now['year'] - 1;
1461 $from['M'] = $now['mon'] - 1;
1462 $from['Y'] = $now['year'];
1465 $from['d'] = date('t', mktime(0, 0, 0, $from['M'], 1, $from['Y']));
1470 $to['d'] = $now['mday'];
1471 $to['M'] = $now['mon'];
1472 $to['Y'] = $now['year'];
1474 $to['i'] = $to['s'] = 59;
1475 $from = self
::intervalAdd('day', -60, $to);
1476 $from = self
::intervalAdd('second', 1, $from);
1480 $to['d'] = $now['mday'];
1481 $to['M'] = $now['mon'];
1482 $to['Y'] = $now['year'];
1484 $to['i'] = $to['s'] = 59;
1485 $from = self
::intervalAdd('day', -30, $to);
1486 $from = self
::intervalAdd('second', 1, $from);
1491 $from['M'] = $now['mon'];;
1492 $from['Y'] = $now['year'];
1493 $to['d'] = $now['mday'];
1494 $to['M'] = $now['mon'];
1495 $to['Y'] = $now['year'];
1497 $to['i'] = $to['s'] = 59;
1502 $to['Y'] = $now['year'];
1503 $to['M'] = $now['mon'];
1504 $to['d'] = date('t', mktime(0, 0, 0, $now['mon'], 1, $now['year']));
1510 if ($now['mon'] == 12) {
1511 $from['M'] = $to['M'] = 1;
1512 $from['Y'] = $to['Y'] = $now['year'] +
1;
1515 $from['M'] = $to['M'] = $now['mon'] +
1;
1516 $from['Y'] = $to['Y'] = $now['year'];
1518 $to['d'] = date('t', mktime(0, 0, 0, $to['M'], 1, $to['Y']));
1522 $from['d'] = $now['mday'];
1523 $from['M'] = $now['mon'];
1524 $from['Y'] = $now['year'];
1526 $from['i'] = $to['s'] = 00;
1527 $to = self
::intervalAdd('day', 30, $from);
1528 $to = self
::intervalAdd('second', -1, $to);
1532 $from['d'] = $now['mday'];
1533 $from['M'] = $now['mon'];
1534 $from['Y'] = $now['year'];
1536 $from['i'] = $to['s'] = 00;
1537 $to = self
::intervalAdd('day', 60, $from);
1538 $to = self
::intervalAdd('second', -1, $to);
1544 $weekFirst = Civi
::settings()->get('weekBegins');
1545 $thisDay = $now['wday'];
1546 if ($weekFirst > $thisDay) {
1547 $diffDay = $thisDay - $weekFirst +
7;
1550 $diffDay = $thisDay - $weekFirst;
1552 switch ($relativeTerm) {
1554 $from['d'] = $now['mday'];
1555 $from['M'] = $now['mon'];
1556 $from['Y'] = $now['year'];
1557 $from = self
::intervalAdd('day', -1 * ($diffDay), $from);
1558 $to = self
::intervalAdd('day', 6, $from);
1562 $from['d'] = $now['mday'];
1563 $from['M'] = $now['mon'];
1564 $from['Y'] = $now['year'];
1565 $from = self
::intervalAdd('day', -1 * ($diffDay) - 7, $from);
1566 $to = self
::intervalAdd('day', 6, $from);
1569 case 'previous_before':
1570 $from['d'] = $now['mday'];
1571 $from['M'] = $now['mon'];
1572 $from['Y'] = $now['year'];
1573 $from = self
::intervalAdd('day', -1 * ($diffDay) - 14, $from);
1574 $to = self
::intervalAdd('day', 6, $from);
1578 $from['d'] = $now['mday'];
1579 $from['M'] = $now['mon'];
1580 $from['Y'] = $now['year'];
1581 $from = self
::intervalAdd('day', -1 * ($diffDay) - 14, $from);
1582 $to = self
::intervalAdd('day', 13, $from);
1586 $to['d'] = $now['mday'];
1587 $to['M'] = $now['mon'];
1588 $to['Y'] = $now['year'];
1589 $to = self
::intervalAdd('day', -1 * ($diffDay) - 1, $to);
1594 $from['d'] = $now['mday'];
1595 $from['M'] = $now['mon'];
1596 $from['Y'] = $now['year'];
1597 $from = self
::intervalAdd('day', -1 * ($diffDay), $from);
1601 case 'greater_previous':
1602 $from['d'] = $now['mday'];
1603 $from['M'] = $now['mon'];
1604 $from['Y'] = $now['year'];
1605 $from = self
::intervalAdd('day', -1 * ($diffDay) - 1, $from);
1610 $to['d'] = $now['mday'];
1611 $to['M'] = $now['mon'];
1612 $to['Y'] = $now['year'];
1614 $to['i'] = $to['s'] = 59;
1615 $from = self
::intervalAdd('day', -7, $to);
1616 $from = self
::intervalAdd('second', 1, $from);
1620 $from['d'] = $now['mday'];
1621 $from['M'] = $now['mon'];
1622 $from['Y'] = $now['year'];
1623 $from = self
::intervalAdd('day', -1 * ($diffDay), $from);
1624 $to['d'] = $now['mday'];
1625 $to['M'] = $now['mon'];
1626 $to['Y'] = $now['year'];
1628 $to['i'] = $to['s'] = 59;
1632 $to['d'] = $now['mday'];
1633 $to['M'] = $now['mon'];
1634 $to['Y'] = $now['year'];
1636 $to = self
::intervalAdd('day', -1 * ($diffDay) +
6, $to);
1641 $from['d'] = $now['mday'];
1642 $from['M'] = $now['mon'];
1643 $from['Y'] = $now['year'];
1644 $from = self
::intervalAdd('day', -1 * ($diffDay) +
7, $from);
1645 $to = self
::intervalAdd('day', 6, $from);
1649 $from['d'] = $now['mday'];
1650 $from['M'] = $now['mon'];
1651 $from['Y'] = $now['year'];
1653 $from['i'] = $to['s'] = 00;
1654 $to = self
::intervalAdd('day', 7, $from);
1655 $to = self
::intervalAdd('second', -1, $to);
1661 switch ($relativeTerm) {
1663 $from['d'] = $to['d'] = $now['mday'];
1664 $from['M'] = $to['M'] = $now['mon'];
1665 $from['Y'] = $to['Y'] = $now['year'];
1669 $from['d'] = $now['mday'];
1670 $from['M'] = $now['mon'];
1671 $from['Y'] = $now['year'];
1672 $from = self
::intervalAdd('day', -1, $from);
1673 $to['d'] = $from['d'];
1674 $to['M'] = $from['M'];
1675 $to['Y'] = $from['Y'];
1678 case 'previous_before':
1679 $from['d'] = $now['mday'];
1680 $from['M'] = $now['mon'];
1681 $from['Y'] = $now['year'];
1682 $from = self
::intervalAdd('day', -2, $from);
1683 $to['d'] = $from['d'];
1684 $to['M'] = $from['M'];
1685 $to['Y'] = $from['Y'];
1689 $from['d'] = $to['d'] = $now['mday'];
1690 $from['M'] = $to['M'] = $now['mon'];
1691 $from['Y'] = $to['Y'] = $now['year'];
1692 $from = self
::intervalAdd('day', -2, $from);
1693 $to = self
::intervalAdd('day', -1, $to);
1697 $to['d'] = $now['mday'];
1698 $to['M'] = $now['mon'];
1699 $to['Y'] = $now['year'];
1704 $from['d'] = $now['mday'];
1705 $from['M'] = $now['mon'];;
1706 $from['Y'] = $now['year'];
1711 $to['d'] = $now['mday'];
1712 $to['M'] = $now['mon'];
1713 $to['Y'] = $now['year'];
1714 $to = self
::intervalAdd('day', 1, $to);
1715 $from['d'] = $to['d'];
1716 $from['M'] = $to['M'];
1717 $from['Y'] = $to['Y'];
1728 if (!empty($
$item)) {
1729 $dateRange[$item] = self
::format($
$item);
1732 $dateRange[$item] = NULL;
1739 * Calculate current fiscal year based on the fiscal month and day.
1741 * @param int $fyDate
1742 * Fiscal start date.
1744 * @param int $fyMonth
1745 * Fiscal Start Month.
1748 * $fy Current Fiscl Year
1750 public static function calculateFiscalYear($fyDate, $fyMonth) {
1751 $date = date("Y-m-d");
1752 $currentYear = date("Y");
1754 // recalculate the date because month 4::04 make the difference
1755 $fiscalYear = explode('-', date("Y-m-d", mktime(0, 0, 0, $fyMonth, $fyDate, $currentYear)));
1756 $fyDate = $fiscalYear[2];
1757 $fyMonth = $fiscalYear[1];
1758 $fyStartDate = date("Y-m-d", mktime(0, 0, 0, $fyMonth, $fyDate, $currentYear));
1760 if ($fyStartDate > $date) {
1761 $fy = intval(intval($currentYear) - 1);
1764 $fy = intval($currentYear);
1770 * Function to process date, convert to mysql format
1772 * @param string $date
1774 * @param string $time
1776 * @param bool|string $returnNullString 'null' needs to be returned
1777 * so that db oject will set null in db
1778 * @param string $format
1779 * Expected return date format.( default is mysql ).
1782 * date format that is excepted by mysql
1784 public static function processDate($date, $time = NULL, $returnNullString = FALSE, $format = 'YmdHis') {
1787 if ($returnNullString) {
1788 $mysqlDate = 'null';
1792 $mysqlDate = date($format, strtotime($date . ' ' . $time));
1799 * Add the metadata about a date field to the field.
1801 * This metadata will work with the call $form->add('datepicker', ...
1803 * @param array $fieldMetaData
1804 * @param array $field
1808 public static function addDateMetadataToField($fieldMetaData, $field) {
1809 if (isset($fieldMetaData['html'])) {
1810 $field['html_type'] = $fieldMetaData['html']['type'];
1811 if ($field['html_type'] === 'Select Date') {
1812 if (!isset($field['date_format'])) {
1813 $dateAttributes = CRM_Core_SelectValues
::date($fieldMetaData['html']['formatType'], NULL, NULL, NULL, 'Input');
1814 $field['start_date_years'] = $dateAttributes['minYear'];
1815 $field['end_date_years'] = $dateAttributes['maxYear'];
1816 $field['date_format'] = $dateAttributes['format'];
1817 $field['is_datetime_field'] = TRUE;
1818 $field['time_format'] = $dateAttributes['time'];
1819 $field['smarty_view_format'] = $dateAttributes['smarty_view_format'];
1821 $field['datepicker']['extra'] = self
::getDatePickerExtra($field);
1822 $field['datepicker']['attributes'] = self
::getDatePickerAttributes($field);
1830 * Get the fields required for the 'extra' parameter when adding a datepicker.
1832 * @param array $field
1836 public static function getDatePickerExtra($field) {
1838 if (isset($field['date_format'])) {
1839 $extra['date'] = $field['date_format'];
1840 $extra['time'] = $field['time_format'];
1842 $thisYear = date('Y');
1843 if (isset($field['start_date_years'])) {
1844 $extra['minDate'] = date('Y-m-d', strtotime('-' . ($thisYear - $field['start_date_years']) . ' years'));
1846 if (isset($field['end_date_years'])) {
1847 $extra['maxDate'] = date('Y-m-d', strtotime('-' . ($thisYear - $field['end_date_years']) . ' years'));
1853 * Get the attributes parameters required for datepicker.
1855 * @param array $field
1859 * Array ready to pass to $this->addForm('datepicker' as attributes.
1861 public static function getDatePickerAttributes(&$field) {
1862 $attributes = array();
1863 $dateAttributes = array(
1864 'start_date_years' => 'minYear',
1865 'end_date_years' => 'maxYear',
1866 'date_format' => 'format',
1868 foreach ($dateAttributes as $dateAttribute => $mapTo) {
1869 if (isset($field[$dateAttribute])) {
1870 $attributes[$mapTo] = $field[$dateAttribute];
1877 * Function to convert mysql to date plugin format.
1879 * @param string $mysqlDate
1882 * @param null $formatType
1883 * @param null $format
1884 * @param null $timeFormat
1889 public static function setDateDefaults($mysqlDate = NULL, $formatType = NULL, $format = NULL, $timeFormat = NULL) {
1890 // if date is not passed assume it as today
1892 $mysqlDate = date('Y-m-d G:i:s');
1895 $config = CRM_Core_Config
::singleton();
1897 // get actual format
1898 $params = array('name' => $formatType);
1900 CRM_Core_DAO
::commonRetrieve('CRM_Core_DAO_PreferencesDate', $params, $values);
1902 if ($values['date_format']) {
1903 $format = $values['date_format'];
1906 if (isset($values['time_format'])) {
1907 $timeFormat = $values['time_format'];
1911 // now we set display date using js, hence we should always setdefault
1912 // 'm/d/Y' format. So that submitted value is alwats mm/dd/YY format
1913 // note that for date display we dynamically create text field
1916 $format = $config->dateInputFormat;
1919 // get actual format
1920 $actualPHPFormats = CRM_Core_SelectValues::datePluginToPHPFormats( );
1921 $dateFormat = CRM_Utils_Array::value( $format, $actualPHPFormats );
1924 $dateFormat = 'm/d/Y';
1925 $date = date($dateFormat, strtotime($mysqlDate));
1928 $timeFormat = $config->timeInputFormat
;
1931 $actualTimeFormat = "g:iA";
1932 $appendZeroLength = 7;
1933 if ($timeFormat > 1) {
1934 $actualTimeFormat = "G:i";
1935 $appendZeroLength = 5;
1938 $time = date($actualTimeFormat, strtotime($mysqlDate));
1940 // need to append zero for hours < 10
1941 if (strlen($time) < $appendZeroLength) {
1942 $time = '0' . $time;
1945 return array($date, $time);
1949 * Function get date format.
1951 * @param string $formatType
1952 * Date name e.g. birth.
1956 public static function getDateFormat($formatType = NULL) {
1959 $format = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_PreferencesDate',
1960 $formatType, 'date_format', 'name'
1965 $config = CRM_Core_Config
::singleton();
1966 $format = $config->dateInputFormat
;
1975 * @return null|string
1977 public static function formatDate($date, $dateType) {
1978 $formattedDate = NULL;
1980 return $formattedDate;
1983 // 1. first convert date to default format.
1984 // 2. append time to default formatted date (might be removed during format)
1985 // 3. validate date / date time.
1986 // 4. If date and time then convert to default date time format.
1989 $dateParams = array($dateKey => $date);
1991 if (CRM_Utils_Date
::convertToDefaultDate($dateParams, $dateType, $dateKey)) {
1992 $dateVal = $dateParams[$dateKey];
1994 if ($dateType == 1) {
1996 if (preg_match("/(\s(([01]\d)|[2][0-3]):([0-5]\d))$/", $date, $matches)) {
1997 $ruleName = 'dateTime';
1998 if (strpos($date, '-') !== FALSE) {
1999 $dateVal .= array_shift($matches);
2005 $valid = CRM_Utils_Rule
::$ruleName($dateVal);
2008 // format date and time to default.
2009 if ($ruleName == 'dateTime') {
2010 $dateVal = CRM_Utils_Date
::customFormat(preg_replace("/(:|\s)?/", "", $dateVal), '%Y%m%d%H%i');
2011 // hack to add seconds
2014 $formattedDate = $dateVal;
2018 return $formattedDate;
2022 * Function to return days of the month.
2026 public static function getCalendarDayOfMonth() {
2028 for ($i = 1; $i <= 31; $i++
) {
2031 $month[$i] = $i . ' / Last day of month';