* @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])) {
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;
}