From: eileen Date: Tue, 2 Jan 2024 01:20:17 +0000 (+1300) Subject: Combine convertToDefaultDate back into only calling function X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=a417a4f8f9e0a7daacf26deddff56e384b04b225;p=civicrm-core.git Combine convertToDefaultDate back into only calling function It is not a logical separation of concerns & makes code more complex rather than simpler --- diff --git a/CRM/Utils/Date.php b/CRM/Utils/Date.php index 2ed91f5305..3ed75e4eb3 100644 --- a/CRM/Utils/Date.php +++ b/CRM/Utils/Date.php @@ -592,9 +592,15 @@ class CRM_Utils_Date { * @param string $dateParam * Index of params. * + * @deprecated since 5.70 will be removed around 5.80. + * + * At time of deprecation only usages were in CiviHR & CiviProspect in classes that + * appeared to be otherwise unmaintained & broken. + * * @return bool */ public static function convertToDefaultDate(&$params, $dateType, $dateParam) { + CRM_Core_Error::deprecatedFunctionWarning('no alternative'); $now = getdate(); $value = ''; if (!empty($params[$dateParam])) { @@ -2196,15 +2202,118 @@ class CRM_Utils_Date { return $timestamp ? date('YmdHis', $timestamp) : NULL; } - $dateKey = 'date'; - $dateParams = [$dateKey => $date]; + $now = getdate(); + // suppress hh:mm or hh:mm:ss if it exists CRM-7957 + $value = preg_replace(self::getTimeRegex(), "", $date); - if (CRM_Utils_Date::convertToDefaultDate($dateParams, $dateType, $dateKey)) { - $dateVal = $dateParams[$dateKey]; - // validate date. - return CRM_Utils_Rule::date($dateVal) ? $dateVal : NULL; + if ($dateType === self::DATE_mm_dd_yy || $dateType === self::DATE_mm_dd_yyyy) { + $formattedDate = explode("/", $value); + if (count($formattedDate) != 3) { + $formattedDate = explode("-", $value); + } + if (count($formattedDate) == 3) { + $year = (int) $formattedDate[2]; + $month = (int) $formattedDate[0]; + $day = (int) $formattedDate[1]; + } + else { + return NULL; + } + } + if ($dateType === self::DATE_Month_dd_yyyy) { + $dateArray = explode(' ', $value); + // ignore comma(,) + $dateArray[1] = (int) substr($dateArray[1], 0, 2); + + $monthInt = 0; + $fullMonths = self::getFullMonthNames(); + foreach ($fullMonths as $key => $val) { + if (strtolower($dateArray[0]) == strtolower($val)) { + $monthInt = $key; + break; + } + } + if (!$monthInt) { + $abbrMonths = self::getAbbrMonthNames(); + foreach ($abbrMonths as $key => $val) { + if (strtolower(trim($dateArray[0], ".")) == strtolower($val)) { + $monthInt = $key; + break; + } + } + } + $year = (int) $dateArray[2]; + $day = (int) $dateArray[1]; + $month = (int) $monthInt; + } + if ($dateType === self::DATE_dd_mon_yy) { + $dateArray = explode('-', $value); + if (count($dateArray) != 3) { + $dateArray = explode('/', $value); + } + + if (count($dateArray) == 3) { + $monthInt = 0; + $fullMonths = self::getFullMonthNames(); + foreach ($fullMonths as $key => $val) { + if (strtolower($dateArray[1]) == strtolower($val)) { + $monthInt = $key; + break; + } + } + if (!$monthInt) { + $abbrMonths = self::getAbbrMonthNames(); + foreach ($abbrMonths as $key => $val) { + if (strtolower(trim($dateArray[1], ".")) == strtolower($val)) { + $monthInt = $key; + break; + } + } + } + if (!$monthInt) { + $monthInt = $dateArray[1]; + } + + $year = (int) $dateArray[2]; + $day = (int) $dateArray[0]; + $month = (int) $monthInt; + } + else { + return NULL; + } + } + if ($dateType === self::DATE_dd_mm_yyyy) { + $formattedDate = explode("/", $value); + if (count($formattedDate) == 3) { + $year = (int) $formattedDate[2]; + $month = (int) $formattedDate[1]; + $day = (int) $formattedDate[0]; + } + else { + return NULL; + } + } + + $month = ($month < 10) ? "0" . "$month" : $month; + $day = ($day < 10) ? "0" . "$day" : $day; + + $year = (int) $year; + if ($year < 100) { + $year = substr($now['year'], 0, 2) * 100 + $year; + if ($year > ($now['year'] + 5)) { + $year = $year - 100; + } + elseif ($year <= ($now['year'] - 95)) { + $year = $year + 100; + } } + $newDate = "$year$month$day"; + // if month is invalid return as error + if ($month !== '00' && $month <= 12) { + // validate date. + return CRM_Utils_Rule::date($newDate) ? $newDate : NULL; + } return NULL; }