_ensureLocale($locale); $output .= $locale->getDayName($this->getPhpDayOfWeek(), true); break; case 'A': $this->_ensureLocale($locale); $output .= $locale->getDayName($this->getPhpDayOfWeek()); break; case 'b': $this->_ensureLocale($locale); $output .= $locale->getMonthName($this->getMonth() - 1, true); break; case 'B': $this->_ensureLocale($locale); $output .= $locale->getMonthName($this->getMonth() - 1); break; case 'p': $this->_ensureLocale($locale); $output .= $locale->getMeridiemName($this->getHour()); break; case 'P': $this->_ensureLocale($locale); $output .= $locale->getMeridiemName($this->getHour(), true); break; case 'C': $output .= sprintf("%02d", intval($this->getYear()/100)); break; case 'd': $output .= sprintf("%02d", $this->getDay()); break; case 'D': $output .= sprintf("%02d/%02d/%02d", $this->getMonth(), $this->getDay(), substr($this->getYear(), 2)); break; case 'e': $output .= $this->getDay(); break; case 'E': $output .= $this->getDateDays(); break; case 'H': $output .= sprintf("%02d", $this->getHour()); break; case 'I': $hour = ($this->getHour() + 1) > 12 ? $this->getHour() - 12 : $this->getHour(); $output .= sprintf("%02d", $hour==0 ? 12 : $hour); break; case 'j': $output .= sprintf("%03d", $this->getDayOfYear()); break; case 'm': $output .= sprintf("%02d",$this->getMonth()); break; case 'M': $output .= sprintf("%02d",$this->getMinute()); break; case 'n': $output .= "\n"; break; case 'O': $offms = $this->getTimeZone()->getOffset($this); $direction = $offms >= 0 ? '+' : '-'; $offmins = abs($offms) / 1000 / 60; $hours = $offmins / 60; $minutes = $offmins % 60; $output .= sprintf("%s%02d:%02d", $direction, $hours, $minutes); break; case 'o': $offms = $this->getTimeZone()->getRawOffset($this); $direction = $offms >= 0 ? '+' : '-'; $offmins = abs($offms) / 1000 / 60; $hours = $offmins / 60; $minutes = $offmins % 60; $output .= sprintf("%s%02d:%02d", $direction, $hours, $minutes); break; case 'r': $hour = ($this->getHour() + 1) > 12 ? $this->getHour() - 12 : $this->getHour(); $output .= sprintf("%02d:%02d:%02d %s", $hour==0 ? 12 : $hour, $this->getMinute(), $this->getSecond(), $this->getHour() >= 12 ? "PM" : "AM"); break; case 'R': $output .= sprintf("%02d:%02d", $this->getHour(), $this->getMinute()); break; case 'S': $output .= sprintf("%02d", $this->getSecond()); break; case 't': $output .= "\t"; break; case 'T': $output .= sprintf("%02d:%02d:%02d", $this->getHour(), $this->getMinute(), $this->getSecond()); break; case 'w': $output .= $this->getPhpDayOfWeek(); break; case 'U': $output .= $this->getWeekOfYear(); break; case 'y': $output .= substr($this->getYear() . '', 2); break; case 'Y': $output .= sprintf("%04d", $this->getYear()); break; case 'Z': $output .= $this->getTimeZone()->isInDaylightTime() ? $this->getTimeZone()->getDSTShortName() : $this->getTimeZone()->getShortName(); break; case '%': $output .= '%'; break; default: $output .= $char . $nextchar; } $strpos++; } return $output; } /** * Tries to guess time values in time string $time_string formatted with $fmt * Returns an array('hour','minute','second','month','day','year') * At this moment only most common tags are supported. */ static function parseTimeString($locale, $time_string, $fmt) { $hour = 0; $minute = 0; $second = 0; $month = 0; $day = 0; $year = 0; if(!($time_array = self :: explodeTimeStringByFormat($time_string, $fmt))) return -1; foreach($time_array as $time_char => $value) { switch($time_char) { case '%p': case '%P': if(strtolower($value) == $locale->getPmName()) $hour += 12; break; case '%I': case '%H': $hour = (int)$value; break; case '%M': $minute = (int)$value; break; case '%S': $second = (int)$value; break; case '%m': $month = (int)$value; break; case '%b': case '%h': if(($index = array_search($value, $locale->getMonthNames(true))) !== false) { if($index !== false) $month = $index + 1; } break; case '%B': if(($index = array_search($value, $locale->getMonthNames())) !== false) { if($index !== false) $month = $index + 1; } break; case '%d': $day = (int)$value; break; case '%Y': $year = (int)$value; break; case '%y': if($value < 40) $year = 2000 + $value; else $year = 1900 + $value; break; case '%T': if ($regs = explode(':', $value)) { $hour = (int)$regs[1]; $minute = (int)$regs[2]; $second = (int)$regs[3]; } break; case '%D': if ($regs = explode('/', $value)) { $hour = (int)$regs[1]; $minute = (int)$regs[2]; $second = (int)$regs[3]; } break; case '%R': if ($regs = explode(':', $value)) { $hour = (int)$regs[1]; $minute = (int)$regs[2]; } break; } } return array('hour' => $hour, 'minute' => $minute, 'second' => $second, 'month' => $month, 'day' => $day, 'year' => $year); } static function explodeTimeStringByFormat($time_string, $fmt) { $fmt_len = strlen($fmt); $time_string_len = strlen($time_string); $time_array = array(); $fmt_pos = 0; $time_string_pos = 0; while(($fmt_pos = strpos($fmt, '%', $fmt_pos)) !== false) { $current_time_char = $fmt{++$fmt_pos}; if(($fmt_pos+1) >= $fmt_len) $delimiter_pos = $time_string_len; elseif($time_string_pos <= $time_string_len) { $current_delimiter = $fmt{++$fmt_pos}; $delimiter_pos = strpos($time_string, $current_delimiter, $time_string_pos); if($delimiter_pos === false) $delimiter_pos = $time_string_len; } $delimiter_len = $delimiter_pos - $time_string_pos; $value = substr($time_string, $time_string_pos, $delimiter_len); if(preg_match("/[-\/]/", $value)) throw new lmbException("Wrong date format: $time_string does not matches $fmt format"); $time_array['%' . $current_time_char] = $value; $time_string_pos += ($delimiter_len + 1); } return $time_array; } protected function _ensureLocale(&$locale) { if(!is_object($locale)) $locale = lmbToolkit :: instance()->getLocaleObject(); } static function localStringToDate($locale, $string, $format = null) { if(!$format) $format = $locale->getShortDateFormat(); $arr = self :: parseTimeString($locale, $string, $format); return new lmbDate($arr['year'], $arr['month'], $arr['day'], $arr['hour'], $arr['minute'], $arr['second']); } static function isLocalStringValid($locale, $string, $format = null) { try { lmbLocaleDate :: localStringToDate($locale, $string, $format); return true; } catch(lmbException $e) { return false; } } } ?>