function CCGetDateRegExp($FormatMask) { global $CCSLocales; $RegExp = false; if (is_array($FormatMask)) { $masks = array("d" => array("(\\d{1,2})", ccsDay), "dd" => array("(\\d{2})", ccsDay), "ddd" => array("(" . join("|", $CCSLocales->GetFormatInfo("WeekdayShortNames")) . ")", ccsWeek), "dddd" => array("(" . join("|", $CCSLocales->GetFormatInfo("WeekdayNames")) . ")", ccsWeek), "w" => array("\\d"), "ww" => array("\\d{1,2}"), "m" => array("(\\d{1,2})", ccsMonth), "mm" => array("(\\d{2})", ccsMonth), "mmm" => array("(" . join("|", $CCSLocales->GetFormatInfo("MonthShortNames")) . ")", ccsShortMonth), "mmmm" => array("(" . join("|", $CCSLocales->GetFormatInfo("MonthNames")) . ")", ccsFullMonth), "y" => array("\\d{1,3}"), "yy" => array("(\\d{2})", ccsYear), "yyyy" => array("(\\d{4})", ccsYear), "q" => array("\\d"), "h" => array("(\\d{1,2})", ccsHour), "hh" => array("(\\d{2})", ccsHour), "H" => array("(\\d{1,2})", ccsHour), "HH" => array("(\\d{2})", ccsHour), "n" => array("(\\d{1,2})", ccsMinute), "nn" => array("(\\d{2})", ccsMinute), "s" => array("(\\d{1,2})", ccsSecond), "ss" => array("(\\d{2})", ccsSecond), "AM/PM" => array("(AM|PM)", ccsAmPm), "am/pm" => array("(am|pm)", ccsAmPm), "A/P" => array("(A|P)", ccsAmPm), "a/p" => array("(a|p)", ccsAmPm), "a/p" => array("(a|p)", ccsAmPm), "tt" => array("(" . $CCSLocales->GetFormatInfo("AMDesignator") . "|" . $CCSLocales->GetFormatInfo("PMDesignator") . ")", ccsAmPm), "GMT" => array("([\\+\\-]\\d{1,2})", ccsGMT), "S" => array("(\\d{1,6})", ccsMilliSecond)); $RegExp[0] = ""; $RegExpIndex = 1; $is_date = false; $is_datetime = false; for ($i = 0; $i < sizeof($FormatMask); $i++) { if ($FormatMask[$i] == "GeneralDate") { $reg = CCGetDateRegExp($CCSLocales->GetFormatInfo("GeneralDate")); $RegExp[0] .= substr($reg[0], 2, strlen($reg[0]) - 5); $is_datetime = true; for ($j = 1; $j < sizeof($reg); $j++) { $RegExp[$RegExpIndex++] = $reg[$j]; } } else { if ($FormatMask[$i] == "LongDate" || $FormatMask[$i] == "ShortDate") { $reg = CCGetDateRegExp($CCSLocales->GetFormatInfo($FormatMask[$i])); $RegExp[0] .= substr($reg[0], 2, strlen($reg[0]) - 5); $is_date = true; for ($j = 1; $j < sizeof($reg); $j++) { $RegExp[$RegExpIndex++] = $reg[$j]; } } else { if ($FormatMask[$i] == "LongTime" || $FormatMask[$i] == "ShortTime") { $reg = CCGetDateRegExp($CCSLocales->GetFormatInfo($FormatMask[$i])); $RegExp[0] .= substr($reg[0], 2, strlen($reg[0]) - 5); for ($j = 1; $j < sizeof($reg); $j++) { $RegExp[$RegExpIndex++] = $reg[$j]; } } else { if (isset($masks[$FormatMask[$i]])) { $MaskArray = $masks[$FormatMask[$i]]; if ($i == 0 && ($MaskArray[1] == ccsYear || $MaskArray[1] == ccsMonth || $MaskArray[1] == ccsFullMonth || $MaskArray[1] == ccsWeek || $MaskArray[1] == ccsDay)) { $is_date = true; } else { if ($is_date && !$is_datetime && $MaskArray[1] == ccsHour) { $is_datetime = true; } } $RegExp[0] .= $MaskArray[0]; if ($is_datetime) { $RegExp[0] .= "?"; } for ($j = 1; $j < sizeof($MaskArray); $j++) { $RegExp[$RegExpIndex++] = $MaskArray[$j]; } } else { if ($is_date && !$is_datetime && $i < sizeof($FormatMask) && $masks[$FormatMask[$i + 1]][1] == ccsHour) { $is_datetime = true; } $RegExp[0] .= CCAddEscape($FormatMask[$i]); if ($is_datetime) { $RegExp[0] .= "?"; } } } } } } $RegExp[0] = str_replace(" ", "\\s*", $RegExp[0]); $RegExp[0] = "/^" . $RegExp[0] . "\$/i"; } return $RegExp; }
function CCParseDate($ParsingDate, $FormatMask) { global $ShortMonths; global $Months; if (is_array($FormatMask) && strlen($ParsingDate)) { $DateArray = array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0); $RegExp = CCGetDateRegExp($FormatMask); $IsValid = preg_match($RegExp[0], $ParsingDate, $matches); for ($i = 1; $i < sizeof($matches); $i++) { $DateArray[$RegExp[$i]] = $matches[$i]; } if ($DateArray[ccsMonth] == 0 && ($DateArray[ccsFullMonth] != 0 || $DateArray[ccsShortMonth] != 0)) { if ($DateArray[ccsFullMonth] != 0) { $DateArray[ccsMonth] = CCGetIndex($Months, $DateArray[ccsFullMonth], true) + 1; } else { if ($DateArray[ccsShortMonth] != 0) { $DateArray[ccsMonth] = CCGetIndex($ShortMonths, $DateArray[ccsShortMonth], true) + 1; } } } if ($DateArray[ccsHour] < 12 && strtoupper($DateArray[ccsAmPm][0]) == "P") { $DateArray[ccsHour] += 12; } if ($DateArray[ccsHour] == 12 && strtoupper($DateArray[ccsAmPm][0]) == "A") { $DateArray[ccsHour] = 0; } if (strlen($DateArray[ccsYear]) == 2) { if ($DateArray[ccsYear] < 70) { $DateArray[ccsYear] = "20" . $DateArray[ccsYear]; } else { $DateArray[ccsYear] = "19" . $DateArray[ccsYear]; } } if ($DateArray[ccsYear] < 1971 && $DateArray[ccsYear] > 0) { $DateArray[ccsAppropriateYear] = $DateArray[ccsYear] + intval((2000 - $DateArray[ccsYear]) / 28) * 28; } else { if ($DateArray[ccsYear] > 2030) { $DateArray[ccsAppropriateYear] = $DateArray[ccsYear] - intval(($DateArray[ccsYear] - 2000) / 28) * 28; } } //$ParsingDate = mktime ($DateArray[ccsHour], $DateArray[ccsMinute], $DateArray[ccsSecond], $DateArray[ccsMonth], $DateArray[ccsDay], $DateArray[ccsAppropriateYear]); $DateArray[ccsTimestamp] = mktime($DateArray[ccsHour], $DateArray[ccsMinute], $DateArray[ccsSecond], $DateArray[ccsMonth], $DateArray[ccsDay], $DateArray[ccsAppropriateYear]); if ($DateArray[ccsTimestamp] < 0) { $ParsingDate = ""; } else { $ParsingDate = $DateArray; } } return $ParsingDate; }