/** * @param string $format * @param string $date * @param bool $includeTime * * @return JalaliDate|JDateTime * * @throw InvalidDateException */ public static function createJalaliFromFormat($format, $date, $includeTime = false) { $dateParts = new DateParts(); $regexp = ''; $matchCount = 0; $escaped = false; $functions = str_split($format); foreach ($functions as $function) { if ($escaped) { $regexp .= preg_quote($function, '/'); $escaped = false; } elseif ($function === '\\') { $escaped = true; } elseif ($function === '.') { $regexp .= '.'; } elseif ($function === '*') { $regexp .= '.*'; } elseif (array_key_exists($function, static::$conversionFunctions)) { $f = static::$conversionFunctions[$function]; $regexp .= '(' . static::$f($dateParts, $matchCount) . ')'; } elseif ($includeTime && array_key_exists($function, static::$timeConversionFunctions)) { $f = static::$timeConversionFunctions[$function]; $regexp .= '(' . static::$f($dateParts, $matchCount) . ')'; } else { $regexp .= preg_quote($function, '/'); } } if ($dateParts->isAmbiguous()) { throw new InvalidDateException($date); } $regexp = "/^{$regexp}\$/"; $date = StringCleaner::digitsToEnglish($date); if (!preg_match($regexp, $date, $matches)) { throw new InvalidDateException($date); } $dateParts->fillWithMatches($matches); return $dateParts->createJalaliDate($includeTime); }