/** * @param $format * @param $str * @return \DateTime */ public static function dateTimeFromFormat($format, $str) { $jd = new jDate(); $pd = $jd->parseFromFormat($format, $str); $gd = jDateTime::toGregorian($pd['year'], $pd['month'], $pd['day']); $date = new \DateTime(); $date->setDate($gd[0], $gd[1], $gd[2]); $date->setTime($pd['hour'], $pd['minute'], $pd['second']); return $date; }
/** * Gets a Unix Timestamp from a textual date time string * @param string $date * @return int */ public static function getTimestampFromString($date) { $timestamp = strtotime($date); // If we are Jalali, then we want to convert from Jalali back to Gregorian. Otherwise assume input is already Gregorian. if (Config::GetSetting('CALENDAR_TYPE') == 'Jalali') { // Split the time stamp into its component parts and pass it to the conversion. $date = trim($date); $split = stripos($date, ' ') > 0 ? explode(' ', $date) : array($date, ''); $dateSplit = explode('-', $split[0]); $date = jDateTime::toGregorian($dateSplit[0], $dateSplit[1], $dateSplit[2]); //Debug::Audit('Converted to Gregorian from Jalali: ' . var_export($date, true)); // Convert that back into a date using strtotime - the date is now Gregorian $timestamp = strtotime($date[0] . '-' . $date[1] . '-' . $date[2] . ' ' . $split[1]); } return $timestamp; }
/** * Converts Jalai DateTime to Gregorian and return it as string. * By Ahmad Pouramini */ public static function toGregorianStr($j_date, $sep = '/') { $arr = explode($sep, $j_date); if (count($arr) < 3 || intval($arr[0]) == 0) { // invalid date return ""; } else { $g_date = jDateTime::toGregorian($arr[2], $arr[1], $arr[0]); } return implode($sep, $g_date); }