Format.php 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Locale
  17. * @subpackage Format
  18. * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @version $Id$
  20. * @license http://framework.zend.com/license/new-bsd New BSD License
  21. */
  22. /**
  23. * include needed classes
  24. */
  25. require_once 'Zend/Locale/Data.php';
  26. /**
  27. * @category Zend
  28. * @package Zend_Locale
  29. * @subpackage Format
  30. * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
  31. * @license http://framework.zend.com/license/new-bsd New BSD License
  32. */
  33. class Zend_Locale_Format
  34. {
  35. const STANDARD = 'auto';
  36. private static $_options = array('date_format' => null,
  37. 'number_format' => null,
  38. 'format_type' => 'iso',
  39. 'fix_date' => false,
  40. 'locale' => null,
  41. 'cache' => null,
  42. 'disableCache' => false,
  43. 'precision' => null);
  44. /**
  45. * Sets class wide options, if no option was given, the actual set options will be returned
  46. * The 'precision' option of a value is used to truncate or stretch extra digits. -1 means not to touch the extra digits.
  47. * The 'locale' option helps when parsing numbers and dates using separators and month names.
  48. * The date format 'format_type' option selects between CLDR/ISO date format specifier tokens and PHP's date() tokens.
  49. * The 'fix_date' option enables or disables heuristics that attempt to correct invalid dates.
  50. * The 'number_format' option can be used to specify a default number format string
  51. * The 'date_format' option can be used to specify a default date format string, but beware of using getDate(),
  52. * checkDateFormat() and getTime() after using setOptions() with a 'format'. To use these four methods
  53. * with the default date format for a locale, use array('date_format' => null, 'locale' => $locale) for their options.
  54. *
  55. * @param array $options Array of options, keyed by option name: format_type = 'iso' | 'php', fix_date = true | false,
  56. * locale = Zend_Locale | locale string, precision = whole number between -1 and 30
  57. * @throws Zend_Locale_Exception
  58. * @return array if no option was given
  59. */
  60. public static function setOptions(array $options = array())
  61. {
  62. self::$_options = self::_checkOptions($options) + self::$_options;
  63. return self::$_options;
  64. }
  65. /**
  66. * Internal function for checking the options array of proper input values
  67. * See {@link setOptions()} for details.
  68. *
  69. * @param array $options Array of options, keyed by option name: format_type = 'iso' | 'php', fix_date = true | false,
  70. * locale = Zend_Locale | locale string, precision = whole number between -1 and 30
  71. * @throws Zend_Locale_Exception
  72. * @return array if no option was given
  73. */
  74. private static function _checkOptions(array $options = array())
  75. {
  76. if (count($options) == 0) {
  77. return self::$_options;
  78. }
  79. foreach ($options as $name => $value) {
  80. $name = strtolower($name);
  81. if ($name !== 'locale') {
  82. if (gettype($value) === 'string') {
  83. $value = strtolower($value);
  84. }
  85. }
  86. switch($name) {
  87. case 'number_format' :
  88. if ($value == Zend_Locale_Format::STANDARD) {
  89. $locale = self::$_options['locale'];
  90. if (isset($options['locale'])) {
  91. $locale = $options['locale'];
  92. }
  93. $options['number_format'] = Zend_Locale_Data::getContent($locale, 'decimalnumber');
  94. } else if ((gettype($value) !== 'string') and ($value !== NULL)) {
  95. require_once 'Zend/Locale/Exception.php';
  96. $stringValue = (string)(is_array($value) ? implode(' ', $value) : $value);
  97. throw new Zend_Locale_Exception("Unknown number format type '" . gettype($value) . "'. "
  98. . "Format '$stringValue' must be a valid number format string.");
  99. }
  100. break;
  101. case 'date_format' :
  102. if ($value == Zend_Locale_Format::STANDARD) {
  103. $locale = self::$_options['locale'];
  104. if (isset($options['locale'])) {
  105. $locale = $options['locale'];
  106. }
  107. $options['date_format'] = Zend_Locale_Format::getDateFormat($locale);
  108. } else if ((gettype($value) !== 'string') and ($value !== NULL)) {
  109. require_once 'Zend/Locale/Exception.php';
  110. $stringValue = (string)(is_array($value) ? implode(' ', $value) : $value);
  111. throw new Zend_Locale_Exception("Unknown dateformat type '" . gettype($value) . "'. "
  112. . "Format '$stringValue' must be a valid ISO or PHP date format string.");
  113. } else {
  114. if (((isset($options['format_type']) === true) and ($options['format_type'] == 'php')) or
  115. ((isset($options['format_type']) === false) and (self::$_options['format_type'] == 'php'))) {
  116. $options['date_format'] = Zend_Locale_Format::convertPhpToIsoFormat($value);
  117. }
  118. }
  119. break;
  120. case 'format_type' :
  121. if (($value != 'php') && ($value != 'iso')) {
  122. require_once 'Zend/Locale/Exception.php';
  123. throw new Zend_Locale_Exception("Unknown date format type '$value'. Only 'iso' and 'php'"
  124. . " are supported.");
  125. }
  126. break;
  127. case 'fix_date' :
  128. if (($value !== true) && ($value !== false)) {
  129. require_once 'Zend/Locale/Exception.php';
  130. throw new Zend_Locale_Exception("Enabling correction of dates must be either true or false"
  131. . "(fix_date='$value').");
  132. }
  133. break;
  134. case 'locale' :
  135. $options['locale'] = Zend_Locale::findLocale($value);
  136. break;
  137. case 'cache' :
  138. if ($value instanceof Zend_Cache_Core) {
  139. Zend_Locale_Data::setCache($value);
  140. }
  141. break;
  142. case 'disablecache' :
  143. Zend_Locale_Data::disableCache($value);
  144. break;
  145. case 'precision' :
  146. if ($value === NULL) {
  147. $value = -1;
  148. }
  149. if (($value < -1) || ($value > 30)) {
  150. require_once 'Zend/Locale/Exception.php';
  151. throw new Zend_Locale_Exception("'$value' precision is not a whole number less than 30.");
  152. }
  153. break;
  154. default:
  155. require_once 'Zend/Locale/Exception.php';
  156. throw new Zend_Locale_Exception("Unknown option: '$name' = '$value'");
  157. break;
  158. }
  159. }
  160. return $options;
  161. }
  162. /**
  163. * Changes the numbers/digits within a given string from one script to another
  164. * 'Decimal' representated the stardard numbers 0-9, if a script does not exist
  165. * an exception will be thrown.
  166. *
  167. * Examples for conversion from Arabic to Latin numerals:
  168. * convertNumerals('١١٠ Tests', 'Arab'); -> returns '100 Tests'
  169. * Example for conversion from Latin to Arabic numerals:
  170. * convertNumerals('100 Tests', 'Latn', 'Arab'); -> returns '١١٠ Tests'
  171. *
  172. * @param string $input String to convert
  173. * @param string $from Script to parse, see {@link Zend_Locale::getScriptList()} for details.
  174. * @param string $to OPTIONAL Script to convert to
  175. * @return string Returns the converted input
  176. * @throws Zend_Locale_Exception
  177. */
  178. public static function convertNumerals($input, $from, $to = null)
  179. {
  180. if (!self::_getUniCodeSupport()) {
  181. trigger_error("Sorry, your PCRE extension does not support UTF8 which is needed for the I18N core", E_USER_NOTICE);
  182. }
  183. $from = strtolower($from);
  184. $source = Zend_Locale_Data::getContent('en', 'numberingsystem', $from);
  185. if (empty($source)) {
  186. require_once 'Zend/Locale/Exception.php';
  187. throw new Zend_Locale_Exception("Unknown script '$from'. Use 'Latn' for digits 0,1,2,3,4,5,6,7,8,9.");
  188. }
  189. if ($to !== null) {
  190. $to = strtolower($to);
  191. $target = Zend_Locale_Data::getContent('en', 'numberingsystem', $to);
  192. if (empty($target)) {
  193. require_once 'Zend/Locale/Exception.php';
  194. throw new Zend_Locale_Exception("Unknown script '$to'. Use 'Latn' for digits 0,1,2,3,4,5,6,7,8,9.");
  195. }
  196. } else {
  197. $target = '0123456789';
  198. }
  199. for ($x = 0; $x < 10; ++$x) {
  200. $asource[$x] = "/" . iconv_substr($source, $x, 1, 'UTF-8') . "/u";
  201. $atarget[$x] = iconv_substr($target, $x, 1, 'UTF-8');
  202. }
  203. return preg_replace($asource, $atarget, $input);
  204. }
  205. /**
  206. * Returns the normalized number from a localized one
  207. * Parsing depends on given locale (grouping and decimal)
  208. *
  209. * Examples for input:
  210. * '2345.4356,1234' = 23455456.1234
  211. * '+23,3452.123' = 233452.123
  212. * '12343 ' = 12343
  213. * '-9456' = -9456
  214. * '0' = 0
  215. *
  216. * @param string $input Input string to parse for numbers
  217. * @param array $options Options: locale, precision. See {@link setOptions()} for details.
  218. * @return string Returns the extracted number
  219. * @throws Zend_Locale_Exception
  220. */
  221. public static function getNumber($input, array $options = array())
  222. {
  223. $options = self::_checkOptions($options) + self::$_options;
  224. if (!is_string($input)) {
  225. return $input;
  226. }
  227. if (!self::isNumber($input, $options)) {
  228. require_once 'Zend/Locale/Exception.php';
  229. throw new Zend_Locale_Exception('No localized value in ' . $input . ' found, or the given number does not match the localized format');
  230. }
  231. // Get correct signs for this locale
  232. $symbols = Zend_Locale_Data::getList($options['locale'],'symbols');
  233. // Change locale input to be default number
  234. if ((strpos($input, $symbols['minus']) !== false) ||
  235. (strpos($input, '-') !== false)) {
  236. $input = strtr($input, array($symbols['minus'] => '', '-' => ''));
  237. $input = '-' . $input;
  238. }
  239. $input = str_replace($symbols['group'],'', $input);
  240. if (strpos($input, $symbols['decimal']) !== false) {
  241. if ($symbols['decimal'] != '.') {
  242. $input = str_replace($symbols['decimal'], ".", $input);
  243. }
  244. $pre = substr($input, strpos($input, '.') + 1);
  245. if ($options['precision'] === null) {
  246. $options['precision'] = strlen($pre);
  247. }
  248. if (strlen($pre) >= $options['precision']) {
  249. $input = substr($input, 0, strlen($input) - strlen($pre) + $options['precision']);
  250. }
  251. if (($options['precision'] == 0) && ($input[strlen($input) - 1] == '.')) {
  252. $input = substr($input, 0, -1);
  253. }
  254. }
  255. return $input;
  256. }
  257. /**
  258. * Returns a locale formatted number depending on the given options.
  259. * The seperation and fraction sign is used from the set locale.
  260. * ##0.# -> 12345.12345 -> 12345.12345
  261. * ##0.00 -> 12345.12345 -> 12345.12
  262. * ##,##0.00 -> 12345.12345 -> 12,345.12
  263. *
  264. * @param string $value Localized number string
  265. * @param array $options Options: number_format, locale, precision. See {@link setOptions()} for details.
  266. * @return string locale formatted number
  267. * @throws Zend_Locale_Exception
  268. */
  269. public static function toNumber($value, array $options = array())
  270. {
  271. // load class within method for speed
  272. require_once 'Zend/Locale/Math.php';
  273. $value = Zend_Locale_Math::floatalize($value);
  274. $value = Zend_Locale_Math::normalize($value);
  275. $options = self::_checkOptions($options) + self::$_options;
  276. $options['locale'] = (string) $options['locale'];
  277. // Get correct signs for this locale
  278. $symbols = Zend_Locale_Data::getList($options['locale'], 'symbols');
  279. $oenc = iconv_get_encoding('internal_encoding');
  280. if (PHP_VERSION_ID < 50600) {
  281. iconv_set_encoding('internal_encoding', 'UTF-8');
  282. } else {
  283. ini_set('default_charset', 'UTF-8');
  284. }
  285. // Get format
  286. $format = $options['number_format'];
  287. if ($format === null) {
  288. $format = Zend_Locale_Data::getContent($options['locale'], 'decimalnumber');
  289. $format = self::_seperateFormat($format, $value, $options['precision']);
  290. if ($options['precision'] !== null) {
  291. $value = Zend_Locale_Math::normalize(Zend_Locale_Math::round($value, $options['precision']));
  292. }
  293. } else {
  294. // seperate negative format pattern when available
  295. $format = self::_seperateFormat($format, $value, $options['precision']);
  296. if (strpos($format, '.')) {
  297. if (is_numeric($options['precision'])) {
  298. $value = Zend_Locale_Math::round($value, $options['precision']);
  299. } else {
  300. if (substr($format, iconv_strpos($format, '.') + 1, 3) == '###') {
  301. $options['precision'] = null;
  302. } else {
  303. $options['precision'] = iconv_strlen(iconv_substr($format, iconv_strpos($format, '.') + 1,
  304. iconv_strrpos($format, '0') - iconv_strpos($format, '.')));
  305. $format = iconv_substr($format, 0, iconv_strpos($format, '.') + 1) . '###'
  306. . iconv_substr($format, iconv_strrpos($format, '0') + 1);
  307. }
  308. }
  309. } else {
  310. $value = Zend_Locale_Math::round($value, 0);
  311. $options['precision'] = 0;
  312. }
  313. $value = Zend_Locale_Math::normalize($value);
  314. }
  315. if (iconv_strpos($format, '0') === false) {
  316. if (PHP_VERSION_ID < 50600) {
  317. iconv_set_encoding('internal_encoding', $oenc);
  318. } else {
  319. ini_set('default_charset', $oenc);
  320. }
  321. require_once 'Zend/Locale/Exception.php';
  322. throw new Zend_Locale_Exception('Wrong format... missing 0');
  323. }
  324. // get number parts
  325. $pos = iconv_strpos($value, '.');
  326. if ($pos !== false) {
  327. if ($options['precision'] === null) {
  328. $precstr = iconv_substr($value, $pos + 1);
  329. } else {
  330. $precstr = iconv_substr($value, $pos + 1, $options['precision']);
  331. if (iconv_strlen($precstr) < $options['precision']) {
  332. $precstr = $precstr . str_pad("0", ($options['precision'] - iconv_strlen($precstr)), "0");
  333. }
  334. }
  335. } else {
  336. if ($options['precision'] > 0) {
  337. $precstr = str_pad("0", ($options['precision']), "0");
  338. }
  339. }
  340. if ($options['precision'] === null) {
  341. if (isset($precstr)) {
  342. $options['precision'] = iconv_strlen($precstr);
  343. } else {
  344. $options['precision'] = 0;
  345. }
  346. }
  347. // get fraction and format lengths
  348. if (strpos($value, '.') !== false) {
  349. $number = substr((string) $value, 0, strpos($value, '.'));
  350. } else {
  351. $number = $value;
  352. }
  353. $prec = call_user_func(Zend_Locale_Math::$sub, $value, $number, $options['precision']);
  354. $prec = Zend_Locale_Math::floatalize($prec);
  355. $prec = Zend_Locale_Math::normalize($prec);
  356. if (iconv_strpos($prec, '-') !== false) {
  357. $prec = iconv_substr($prec, 1);
  358. }
  359. if (($prec == 0) and ($options['precision'] > 0)) {
  360. $prec = "0.0";
  361. }
  362. if (($options['precision'] + 2) > iconv_strlen($prec)) {
  363. $prec = str_pad((string) $prec, $options['precision'] + 2, "0", STR_PAD_RIGHT);
  364. }
  365. if (iconv_strpos($number, '-') !== false) {
  366. $number = iconv_substr($number, 1);
  367. }
  368. $group = iconv_strrpos($format, ',');
  369. $group2 = iconv_strpos ($format, ',');
  370. $point = iconv_strpos ($format, '0');
  371. // Add fraction
  372. $rest = "";
  373. if (iconv_strpos($format, '.')) {
  374. $rest = iconv_substr($format, iconv_strpos($format, '.') + 1);
  375. $length = iconv_strlen($rest);
  376. for($x = 0; $x < $length; ++$x) {
  377. if (($rest[0] == '0') || ($rest[0] == '#')) {
  378. $rest = iconv_substr($rest, 1);
  379. }
  380. }
  381. $format = iconv_substr($format, 0, iconv_strlen($format) - iconv_strlen($rest));
  382. }
  383. if ($options['precision'] == '0') {
  384. if (iconv_strrpos($format, '-') != 0) {
  385. $format = iconv_substr($format, 0, $point)
  386. . iconv_substr($format, iconv_strrpos($format, '#') + 2);
  387. } else {
  388. $format = iconv_substr($format, 0, $point);
  389. }
  390. } else {
  391. $format = iconv_substr($format, 0, $point) . $symbols['decimal']
  392. . iconv_substr($prec, 2);
  393. }
  394. $format .= $rest;
  395. // Add seperation
  396. if ($group == 0) {
  397. // no seperation
  398. $format = $number . iconv_substr($format, $point);
  399. } else if ($group == $group2) {
  400. // only 1 seperation
  401. $seperation = ($point - $group);
  402. for ($x = iconv_strlen($number); $x > $seperation; $x -= $seperation) {
  403. if (iconv_substr($number, 0, $x - $seperation) !== "") {
  404. $number = iconv_substr($number, 0, $x - $seperation) . $symbols['group']
  405. . iconv_substr($number, $x - $seperation);
  406. }
  407. }
  408. $format = iconv_substr($format, 0, iconv_strpos($format, '#')) . $number . iconv_substr($format, $point);
  409. } else {
  410. // 2 seperations
  411. if (iconv_strlen($number) > ($point - $group)) {
  412. $seperation = ($point - $group);
  413. $number = iconv_substr($number, 0, iconv_strlen($number) - $seperation) . $symbols['group']
  414. . iconv_substr($number, iconv_strlen($number) - $seperation);
  415. if ((iconv_strlen($number) - 1) > ($point - $group + 1)) {
  416. $seperation2 = ($group - $group2 - 1);
  417. for ($x = iconv_strlen($number) - $seperation2 - 2; $x > $seperation2; $x -= $seperation2) {
  418. $number = iconv_substr($number, 0, $x - $seperation2) . $symbols['group']
  419. . iconv_substr($number, $x - $seperation2);
  420. }
  421. }
  422. }
  423. $format = iconv_substr($format, 0, iconv_strpos($format, '#')) . $number . iconv_substr($format, $point);
  424. }
  425. // set negative sign
  426. if (call_user_func(Zend_Locale_Math::$comp, $value, 0, $options['precision']) < 0) {
  427. if (iconv_strpos($format, '-') === false) {
  428. $format = $symbols['minus'] . $format;
  429. } else {
  430. $format = str_replace('-', $symbols['minus'], $format);
  431. }
  432. }
  433. if (PHP_VERSION_ID < 50600) {
  434. iconv_set_encoding('internal_encoding', $oenc);
  435. } else {
  436. ini_set('default_charset', $oenc);
  437. }
  438. return (string) $format;
  439. }
  440. /**
  441. * @param string $format
  442. * @param string $value
  443. * @param int $precision
  444. * @return string
  445. */
  446. private static function _seperateFormat($format, $value, $precision)
  447. {
  448. if (iconv_strpos($format, ';') !== false) {
  449. if (call_user_func(Zend_Locale_Math::$comp, $value, 0, $precision) < 0) {
  450. $tmpformat = iconv_substr($format, iconv_strpos($format, ';') + 1);
  451. if ($tmpformat[0] == '(') {
  452. $format = iconv_substr($format, 0, iconv_strpos($format, ';'));
  453. } else {
  454. $format = $tmpformat;
  455. }
  456. } else {
  457. $format = iconv_substr($format, 0, iconv_strpos($format, ';'));
  458. }
  459. }
  460. return $format;
  461. }
  462. /**
  463. * Checks if the input contains a normalized or localized number
  464. *
  465. * @param string $input Localized number string
  466. * @param array $options Options: locale. See {@link setOptions()} for details.
  467. * @return boolean Returns true if a number was found
  468. */
  469. public static function isNumber($input, array $options = array())
  470. {
  471. if (!self::_getUniCodeSupport()) {
  472. trigger_error("Sorry, your PCRE extension does not support UTF8 which is needed for the I18N core", E_USER_NOTICE);
  473. }
  474. $options = self::_checkOptions($options) + self::$_options;
  475. // Get correct signs for this locale
  476. $symbols = Zend_Locale_Data::getList($options['locale'],'symbols');
  477. $regexs = Zend_Locale_Format::_getRegexForType('decimalnumber', $options);
  478. $regexs = array_merge($regexs, Zend_Locale_Format::_getRegexForType('scientificnumber', $options));
  479. if (!empty($input) && ($input[0] == $symbols['decimal'])) {
  480. $input = 0 . $input;
  481. }
  482. foreach ($regexs as $regex) {
  483. preg_match($regex, $input, $found);
  484. if (isset($found[0])) {
  485. return true;
  486. }
  487. }
  488. return false;
  489. }
  490. /**
  491. * Internal method to convert cldr number syntax into regex
  492. *
  493. * @param string $type
  494. * @param array $options Options: locale. See {@link setOptions()} for details.
  495. * @return string
  496. * @throws Zend_Locale_Exception
  497. */
  498. private static function _getRegexForType($type, $options)
  499. {
  500. $decimal = Zend_Locale_Data::getContent($options['locale'], $type);
  501. $decimal = preg_replace('/[^#0,;\.\-Ee]/u', '',$decimal);
  502. $patterns = explode(';', $decimal);
  503. if (count($patterns) == 1) {
  504. $patterns[1] = '-' . $patterns[0];
  505. }
  506. $symbols = Zend_Locale_Data::getList($options['locale'],'symbols');
  507. foreach($patterns as $pkey => $pattern) {
  508. $regex[$pkey] = '/^';
  509. $rest = 0;
  510. $end = null;
  511. if (strpos($pattern, '.') !== false) {
  512. $end = substr($pattern, strpos($pattern, '.') + 1);
  513. $pattern = substr($pattern, 0, -strlen($end) - 1);
  514. }
  515. if (strpos($pattern, ',') !== false) {
  516. $parts = explode(',', $pattern);
  517. $count = count($parts);
  518. foreach($parts as $key => $part) {
  519. switch ($part) {
  520. case '#':
  521. case '-#':
  522. if ($part[0] == '-') {
  523. $regex[$pkey] .= '[' . $symbols['minus'] . '-]{0,1}';
  524. } else {
  525. $regex[$pkey] .= '[' . $symbols['plus'] . '+]{0,1}';
  526. }
  527. if (($parts[$key + 1]) == '##0') {
  528. $regex[$pkey] .= '[0-9]{1,3}';
  529. } else if (($parts[$key + 1]) == '##') {
  530. $regex[$pkey] .= '[0-9]{1,2}';
  531. } else {
  532. throw new Zend_Locale_Exception('Unsupported token for numberformat (Pos 1):"' . $pattern . '"');
  533. }
  534. break;
  535. case '##':
  536. if ($parts[$key + 1] == '##0') {
  537. $regex[$pkey] .= '(\\' . $symbols['group'] . '{0,1}[0-9]{2})*';
  538. } else {
  539. throw new Zend_Locale_Exception('Unsupported token for numberformat (Pos 2):"' . $pattern . '"');
  540. }
  541. break;
  542. case '##0':
  543. if ($parts[$key - 1] == '##') {
  544. $regex[$pkey] .= '[0-9]';
  545. } else if (($parts[$key - 1] == '#') || ($parts[$key - 1] == '-#')) {
  546. $regex[$pkey] .= '(\\' . $symbols['group'] . '{0,1}[0-9]{3})*';
  547. } else {
  548. throw new Zend_Locale_Exception('Unsupported token for numberformat (Pos 3):"' . $pattern . '"');
  549. }
  550. break;
  551. case '#0':
  552. if ($key == 0) {
  553. $regex[$pkey] .= '[0-9]*';
  554. } else {
  555. throw new Zend_Locale_Exception('Unsupported token for numberformat (Pos 4):"' . $pattern . '"');
  556. }
  557. break;
  558. }
  559. }
  560. }
  561. if (strpos($pattern, 'E') !== false) {
  562. if (($pattern == '#E0') || ($pattern == '#E00')) {
  563. $regex[$pkey] .= '[' . $symbols['plus']. '+]{0,1}[0-9]{1,}(\\' . $symbols['decimal'] . '[0-9]{1,})*[eE][' . $symbols['plus']. '+]{0,1}[0-9]{1,}';
  564. } else if (($pattern == '-#E0') || ($pattern == '-#E00')) {
  565. $regex[$pkey] .= '[' . $symbols['minus']. '-]{0,1}[0-9]{1,}(\\' . $symbols['decimal'] . '[0-9]{1,})*[eE][' . $symbols['minus']. '-]{0,1}[0-9]{1,}';
  566. } else {
  567. throw new Zend_Locale_Exception('Unsupported token for numberformat (Pos 5):"' . $pattern . '"');
  568. }
  569. }
  570. if (!empty($end)) {
  571. if ($end == '###') {
  572. $regex[$pkey] .= '(\\' . $symbols['decimal'] . '{1}[0-9]{1,}){0,1}';
  573. } else if ($end == '###-') {
  574. $regex[$pkey] .= '(\\' . $symbols['decimal'] . '{1}[0-9]{1,}){0,1}[' . $symbols['minus']. '-]';
  575. } else {
  576. throw new Zend_Locale_Exception('Unsupported token for numberformat (Pos 6):"' . $pattern . '"');
  577. }
  578. }
  579. $regex[$pkey] .= '$/u';
  580. }
  581. return $regex;
  582. }
  583. /**
  584. * Alias for getNumber
  585. *
  586. * @param string $input Number to localize
  587. * @param array $options Options: locale, precision. See {@link setOptions()} for details.
  588. * @return float
  589. */
  590. public static function getFloat($input, array $options = array())
  591. {
  592. return floatval(self::getNumber($input, $options));
  593. }
  594. /**
  595. * Returns a locale formatted integer number
  596. * Alias for toNumber()
  597. *
  598. * @param string $value Number to normalize
  599. * @param array $options Options: locale, precision. See {@link setOptions()} for details.
  600. * @return string Locale formatted number
  601. */
  602. public static function toFloat($value, array $options = array())
  603. {
  604. $options['number_format'] = Zend_Locale_Format::STANDARD;
  605. return self::toNumber($value, $options);
  606. }
  607. /**
  608. * Returns if a float was found
  609. * Alias for isNumber()
  610. *
  611. * @param string $value Localized number string
  612. * @param array $options Options: locale. See {@link setOptions()} for details.
  613. * @return boolean Returns true if a number was found
  614. */
  615. public static function isFloat($value, array $options = array())
  616. {
  617. return self::isNumber($value, $options);
  618. }
  619. /**
  620. * Returns the first found integer from an string
  621. * Parsing depends on given locale (grouping and decimal)
  622. *
  623. * Examples for input:
  624. * ' 2345.4356,1234' = 23455456
  625. * '+23,3452.123' = 233452
  626. * ' 12343 ' = 12343
  627. * '-9456km' = -9456
  628. * '0' = 0
  629. * '(-){0,1}(\d+(\.){0,1})*(\,){0,1})\d+'
  630. *
  631. * @param string $input Input string to parse for numbers
  632. * @param array $options Options: locale. See {@link setOptions()} for details.
  633. * @return integer Returns the extracted number
  634. */
  635. public static function getInteger($input, array $options = array())
  636. {
  637. $options['precision'] = 0;
  638. return intval(self::getFloat($input, $options));
  639. }
  640. /**
  641. * Returns a localized number
  642. *
  643. * @param string $value Number to normalize
  644. * @param array $options Options: locale. See {@link setOptions()} for details.
  645. * @return string Locale formatted number
  646. */
  647. public static function toInteger($value, array $options = array())
  648. {
  649. $options['precision'] = 0;
  650. $options['number_format'] = Zend_Locale_Format::STANDARD;
  651. return self::toNumber($value, $options);
  652. }
  653. /**
  654. * Returns if a integer was found
  655. *
  656. * @param string $value Localized number string
  657. * @param array $options Options: locale. See {@link setOptions()} for details.
  658. * @return boolean Returns true if a integer was found
  659. */
  660. public static function isInteger($value, array $options = array())
  661. {
  662. if (!self::isNumber($value, $options)) {
  663. return false;
  664. }
  665. if (self::getInteger($value, $options) == self::getFloat($value, $options)) {
  666. return true;
  667. }
  668. return false;
  669. }
  670. /**
  671. * Converts a format string from PHP's date format to ISO format
  672. * Remember that Zend Date always returns localized string, so a month name which returns the english
  673. * month in php's date() will return the translated month name with this function... use 'en' as locale
  674. * if you are in need of the original english names
  675. *
  676. * The conversion has the following restrictions:
  677. * 'a', 'A' - Meridiem is not explicit upper/lowercase, you have to upper/lowercase the translated value yourself
  678. *
  679. * @param string $format Format string in PHP's date format
  680. * @return string Format string in ISO format
  681. */
  682. public static function convertPhpToIsoFormat($format)
  683. {
  684. if ($format === null) {
  685. return null;
  686. }
  687. $convert = array('d' => 'dd' , 'D' => 'EE' , 'j' => 'd' , 'l' => 'EEEE', 'N' => 'eee' , 'S' => 'SS' ,
  688. 'w' => 'e' , 'z' => 'D' , 'W' => 'ww' , 'F' => 'MMMM', 'm' => 'MM' , 'M' => 'MMM' ,
  689. 'n' => 'M' , 't' => 'ddd' , 'L' => 'l' , 'o' => 'YYYY', 'Y' => 'yyyy', 'y' => 'yy' ,
  690. 'a' => 'a' , 'A' => 'a' , 'B' => 'B' , 'g' => 'h' , 'G' => 'H' , 'h' => 'hh' ,
  691. 'H' => 'HH' , 'i' => 'mm' , 's' => 'ss' , 'e' => 'zzzz', 'I' => 'I' , 'O' => 'Z' ,
  692. 'P' => 'ZZZZ', 'T' => 'z' , 'Z' => 'X' , 'c' => 'yyyy-MM-ddTHH:mm:ssZZZZ',
  693. 'r' => 'r' , 'U' => 'U');
  694. $values = str_split($format);
  695. foreach ($values as $key => $value) {
  696. if (isset($convert[$value]) === true) {
  697. $values[$key] = $convert[$value];
  698. }
  699. }
  700. return implode($values);
  701. }
  702. /**
  703. * Parse date and split in named array fields
  704. *
  705. * @param string $date Date string to parse
  706. * @param array $options Options: format_type, fix_date, locale, date_format. See {@link setOptions()} for details.
  707. * @return array Possible array members: day, month, year, hour, minute, second, fixed, format
  708. * @throws Zend_Locale_Exception
  709. */
  710. private static function _parseDate($date, $options)
  711. {
  712. if (!self::_getUniCodeSupport()) {
  713. trigger_error("Sorry, your PCRE extension does not support UTF8 which is needed for the I18N core", E_USER_NOTICE);
  714. }
  715. $options = self::_checkOptions($options) + self::$_options;
  716. $test = array('h', 'H', 'm', 's', 'y', 'Y', 'M', 'd', 'D', 'E', 'S', 'l', 'B', 'I',
  717. 'X', 'r', 'U', 'G', 'w', 'e', 'a', 'A', 'Z', 'z', 'v');
  718. $format = $options['date_format'];
  719. $number = $date; // working copy
  720. $result['date_format'] = $format; // save the format used to normalize $number (convenience)
  721. $result['locale'] = $options['locale']; // save the locale used to normalize $number (convenience)
  722. $oenc = iconv_get_encoding('internal_encoding');
  723. if (PHP_VERSION_ID < 50600) {
  724. iconv_set_encoding('internal_encoding', 'UTF-8');
  725. } else {
  726. ini_set('default_charset', 'UTF-8');
  727. }
  728. $day = iconv_strpos($format, 'd');
  729. $month = iconv_strpos($format, 'M');
  730. $year = iconv_strpos($format, 'y');
  731. $hour = iconv_strpos($format, 'H');
  732. $min = iconv_strpos($format, 'm');
  733. $sec = iconv_strpos($format, 's');
  734. $am = null;
  735. if ($hour === false) {
  736. $hour = iconv_strpos($format, 'h');
  737. }
  738. if ($year === false) {
  739. $year = iconv_strpos($format, 'Y');
  740. }
  741. if ($day === false) {
  742. $day = iconv_strpos($format, 'E');
  743. if ($day === false) {
  744. $day = iconv_strpos($format, 'D');
  745. }
  746. }
  747. if ($day !== false) {
  748. $parse[$day] = 'd';
  749. if (!empty($options['locale']) && ($options['locale'] !== 'root') &&
  750. (!is_object($options['locale']) || ((string) $options['locale'] !== 'root'))) {
  751. // erase day string
  752. $daylist = Zend_Locale_Data::getList($options['locale'], 'day');
  753. foreach($daylist as $key => $name) {
  754. if (iconv_strpos($number, $name) !== false) {
  755. $number = str_replace($name, "EEEE", $number);
  756. break;
  757. }
  758. }
  759. }
  760. }
  761. $position = false;
  762. if ($month !== false) {
  763. $parse[$month] = 'M';
  764. if (!empty($options['locale']) && ($options['locale'] !== 'root') &&
  765. (!is_object($options['locale']) || ((string) $options['locale'] !== 'root'))) {
  766. // prepare to convert month name to their numeric equivalents, if requested,
  767. // and we have a $options['locale']
  768. $position = self::_replaceMonth($number, Zend_Locale_Data::getList($options['locale'],
  769. 'month'));
  770. if ($position === false) {
  771. $position = self::_replaceMonth($number, Zend_Locale_Data::getList($options['locale'],
  772. 'month', array('gregorian', 'format', 'abbreviated')));
  773. }
  774. }
  775. }
  776. if ($year !== false) {
  777. $parse[$year] = 'y';
  778. }
  779. if ($hour !== false) {
  780. $parse[$hour] = 'H';
  781. }
  782. if ($min !== false) {
  783. $parse[$min] = 'm';
  784. }
  785. if ($sec !== false) {
  786. $parse[$sec] = 's';
  787. }
  788. if (empty($parse)) {
  789. if (PHP_VERSION_ID < 50600) {
  790. iconv_set_encoding('internal_encoding', $oenc);
  791. } else {
  792. ini_set('default_charset', $oenc);
  793. }
  794. require_once 'Zend/Locale/Exception.php';
  795. throw new Zend_Locale_Exception("Unknown date format, neither date nor time in '" . $format . "' found");
  796. }
  797. ksort($parse);
  798. // get daytime
  799. if (iconv_strpos($format, 'a') !== false) {
  800. if (iconv_strpos(strtoupper($number), strtoupper(Zend_Locale_Data::getContent($options['locale'], 'am'))) !== false) {
  801. $am = true;
  802. } else if (iconv_strpos(strtoupper($number), strtoupper(Zend_Locale_Data::getContent($options['locale'], 'pm'))) !== false) {
  803. $am = false;
  804. }
  805. }
  806. // split number parts
  807. $split = false;
  808. preg_match_all('/\d+/u', $number, $splitted);
  809. if (count($splitted[0]) == 0) {
  810. if (PHP_VERSION_ID < 50600) {
  811. iconv_set_encoding('internal_encoding', $oenc);
  812. } else {
  813. ini_set('default_charset', $oenc);
  814. }
  815. require_once 'Zend/Locale/Exception.php';
  816. throw new Zend_Locale_Exception("No date part in '$date' found.");
  817. }
  818. if (count($splitted[0]) == 1) {
  819. $split = 0;
  820. }
  821. $cnt = 0;
  822. foreach($parse as $key => $value) {
  823. switch($value) {
  824. case 'd':
  825. if ($split === false) {
  826. if (count($splitted[0]) > $cnt) {
  827. $result['day'] = $splitted[0][$cnt];
  828. }
  829. } else {
  830. $result['day'] = iconv_substr($splitted[0][0], $split, 2);
  831. $split += 2;
  832. }
  833. ++$cnt;
  834. break;
  835. case 'M':
  836. if ($split === false) {
  837. if (count($splitted[0]) > $cnt) {
  838. $result['month'] = $splitted[0][$cnt];
  839. }
  840. } else {
  841. $result['month'] = iconv_substr($splitted[0][0], $split, 2);
  842. $split += 2;
  843. }
  844. ++$cnt;
  845. break;
  846. case 'y':
  847. $length = 2;
  848. if ((iconv_substr($format, $year, 4) == 'yyyy')
  849. || (iconv_substr($format, $year, 4) == 'YYYY')) {
  850. $length = 4;
  851. }
  852. if ($split === false) {
  853. if (count($splitted[0]) > $cnt) {
  854. $result['year'] = $splitted[0][$cnt];
  855. }
  856. } else {
  857. $result['year'] = iconv_substr($splitted[0][0], $split, $length);
  858. $split += $length;
  859. }
  860. ++$cnt;
  861. break;
  862. case 'H':
  863. if ($split === false) {
  864. if (count($splitted[0]) > $cnt) {
  865. $result['hour'] = $splitted[0][$cnt];
  866. }
  867. } else {
  868. $result['hour'] = iconv_substr($splitted[0][0], $split, 2);
  869. $split += 2;
  870. }
  871. ++$cnt;
  872. break;
  873. case 'm':
  874. if ($split === false) {
  875. if (count($splitted[0]) > $cnt) {
  876. $result['minute'] = $splitted[0][$cnt];
  877. }
  878. } else {
  879. $result['minute'] = iconv_substr($splitted[0][0], $split, 2);
  880. $split += 2;
  881. }
  882. ++$cnt;
  883. break;
  884. case 's':
  885. if ($split === false) {
  886. if (count($splitted[0]) > $cnt) {
  887. $result['second'] = $splitted[0][$cnt];
  888. }
  889. } else {
  890. $result['second'] = iconv_substr($splitted[0][0], $split, 2);
  891. $split += 2;
  892. }
  893. ++$cnt;
  894. break;
  895. }
  896. }
  897. // AM/PM correction
  898. if ($hour !== false) {
  899. if (($am === true) and ($result['hour'] == 12)){
  900. $result['hour'] = 0;
  901. } else if (($am === false) and ($result['hour'] != 12)) {
  902. $result['hour'] += 12;
  903. }
  904. }
  905. if ($options['fix_date'] === true) {
  906. $result['fixed'] = 0; // nothing has been "fixed" by swapping date parts around (yet)
  907. }
  908. if ($day !== false) {
  909. // fix false month
  910. if (isset($result['day']) and isset($result['month'])) {
  911. if (($position !== false) and ((iconv_strpos($date, $result['day']) === false) or
  912. (isset($result['year']) and (iconv_strpos($date, $result['year']) === false)))) {
  913. if ($options['fix_date'] !== true) {
  914. if (PHP_VERSION_ID < 50600) {
  915. iconv_set_encoding('internal_encoding', $oenc);
  916. } else {
  917. ini_set('default_charset', $oenc);
  918. }
  919. require_once 'Zend/Locale/Exception.php';
  920. throw new Zend_Locale_Exception("Unable to parse date '$date' using '" . $format
  921. . "' (false month, $position, $month)");
  922. }
  923. $temp = $result['day'];
  924. $result['day'] = $result['month'];
  925. $result['month'] = $temp;
  926. $result['fixed'] = 1;
  927. }
  928. }
  929. // fix switched values d <> y
  930. if (isset($result['day']) and isset($result['year'])) {
  931. if ($result['day'] > 31) {
  932. if ($options['fix_date'] !== true) {
  933. if (PHP_VERSION_ID < 50600) {
  934. iconv_set_encoding('internal_encoding', $oenc);
  935. } else {
  936. ini_set('default_charset', $oenc);
  937. }
  938. require_once 'Zend/Locale/Exception.php';
  939. throw new Zend_Locale_Exception("Unable to parse date '$date' using '"
  940. . $format . "' (d <> y)");
  941. }
  942. $temp = $result['year'];
  943. $result['year'] = $result['day'];
  944. $result['day'] = $temp;
  945. $result['fixed'] = 2;
  946. }
  947. }
  948. // fix switched values M <> y
  949. if (isset($result['month']) and isset($result['year'])) {
  950. if ($result['month'] > 31) {
  951. if ($options['fix_date'] !== true) {
  952. if (PHP_VERSION_ID < 50600) {
  953. iconv_set_encoding('internal_encoding', $oenc);
  954. } else {
  955. ini_set('default_charset', $oenc);
  956. }
  957. require_once 'Zend/Locale/Exception.php';
  958. throw new Zend_Locale_Exception("Unable to parse date '$date' using '"
  959. . $format . "' (M <> y)");
  960. }
  961. $temp = $result['year'];
  962. $result['year'] = $result['month'];
  963. $result['month'] = $temp;
  964. $result['fixed'] = 3;
  965. }
  966. }
  967. // fix switched values M <> d
  968. if (isset($result['month']) and isset($result['day'])) {
  969. if ($result['month'] > 12) {
  970. if ($options['fix_date'] !== true || $result['month'] > 31) {
  971. if (PHP_VERSION_ID < 50600) {
  972. iconv_set_encoding('internal_encoding', $oenc);
  973. } else {
  974. ini_set('default_charset', $oenc);
  975. }
  976. require_once 'Zend/Locale/Exception.php';
  977. throw new Zend_Locale_Exception("Unable to parse date '$date' using '"
  978. . $format . "' (M <> d)");
  979. }
  980. $temp = $result['day'];
  981. $result['day'] = $result['month'];
  982. $result['month'] = $temp;
  983. $result['fixed'] = 4;
  984. }
  985. }
  986. }
  987. if (isset($result['year'])) {
  988. if (((iconv_strlen($result['year']) == 2) && ($result['year'] < 10)) ||
  989. (((iconv_strpos($format, 'yy') !== false) && (iconv_strpos($format, 'yyyy') === false)) ||
  990. ((iconv_strpos($format, 'YY') !== false) && (iconv_strpos($format, 'YYYY') === false)))) {
  991. if (($result['year'] >= 0) && ($result['year'] < 100)) {
  992. if ($result['year'] < 70) {
  993. $result['year'] = (int) $result['year'] + 100;
  994. }
  995. $result['year'] = (int) $result['year'] + 1900;
  996. }
  997. }
  998. }
  999. if (PHP_VERSION_ID < 50600) {
  1000. iconv_set_encoding('internal_encoding', $oenc);
  1001. } else {
  1002. ini_set('default_charset', $oenc);
  1003. }
  1004. return $result;
  1005. }
  1006. /**
  1007. * Search $number for a month name found in $monthlist, and replace if found.
  1008. *
  1009. * @param string $number Date string (modified)
  1010. * @param array $monthlist List of month names
  1011. *
  1012. * @return int|false Position of replaced string (false if nothing replaced)
  1013. */
  1014. protected static function _replaceMonth(&$number, $monthlist)
  1015. {
  1016. // If $locale was invalid, $monthlist will default to a "root" identity
  1017. // mapping for each month number from 1 to 12.
  1018. // If no $locale was given, or $locale was invalid, do not use this identity mapping to normalize.
  1019. // Otherwise, translate locale aware month names in $number to their numeric equivalents.
  1020. $position = false;
  1021. if ($monthlist && $monthlist[1] != 1) {
  1022. foreach($monthlist as $key => $name) {
  1023. if (($position = iconv_strpos($number, $name, 0, 'UTF-8')) !== false) {
  1024. $number = str_ireplace($name, $key, $number);
  1025. return $position;
  1026. }
  1027. }
  1028. }
  1029. return false;
  1030. }
  1031. /**
  1032. * Returns the default date format for $locale.
  1033. *
  1034. * @param string|Zend_Locale $locale OPTIONAL Locale of $number, possibly in string form (e.g. 'de_AT')
  1035. * @return string format
  1036. * @throws Zend_Locale_Exception throws an exception when locale data is broken
  1037. */
  1038. public static function getDateFormat($locale = null)
  1039. {
  1040. $format = Zend_Locale_Data::getContent($locale, 'date');
  1041. if (empty($format)) {
  1042. require_once 'Zend/Locale/Exception.php';
  1043. throw new Zend_Locale_Exception("failed to receive data from locale $locale");
  1044. }
  1045. return $format;
  1046. }
  1047. /**
  1048. * Returns an array with the normalized date from an locale date
  1049. * a input of 10.01.2006 without a $locale would return:
  1050. * array ('day' => 10, 'month' => 1, 'year' => 2006)
  1051. * The 'locale' option is only used to convert human readable day
  1052. * and month names to their numeric equivalents.
  1053. * The 'format' option allows specification of self-defined date formats,
  1054. * when not using the default format for the 'locale'.
  1055. *
  1056. * @param string $date Date string
  1057. * @param array $options Options: format_type, fix_date, locale, date_format. See {@link setOptions()} for details.
  1058. * @return array Possible array members: day, month, year, hour, minute, second, fixed, format
  1059. */
  1060. public static function getDate($date, array $options = array())
  1061. {
  1062. $options = self::_checkOptions($options) + self::$_options;
  1063. if (empty($options['date_format'])) {
  1064. $options['format_type'] = 'iso';
  1065. $options['date_format'] = self::getDateFormat($options['locale']);
  1066. }
  1067. return self::_parseDate($date, $options);
  1068. }
  1069. /**
  1070. * Returns if the given datestring contains all date parts from the given format.
  1071. * If no format is given, the default date format from the locale is used
  1072. * If you want to check if the date is a proper date you should use Zend_Date::isDate()
  1073. *
  1074. * @param string $date Date string
  1075. * @param array $options Options: format_type, fix_date, locale, date_format. See {@link setOptions()} for details.
  1076. * @return boolean
  1077. */
  1078. public static function checkDateFormat($date, array $options = array())
  1079. {
  1080. try {
  1081. $date = self::getDate($date, $options);
  1082. } catch (Exception $e) {
  1083. return false;
  1084. }
  1085. if (empty($options['date_format'])) {
  1086. $options['format_type'] = 'iso';
  1087. $options['date_format'] = self::getDateFormat(isset($options['locale']) ? $options['locale'] : null);
  1088. }
  1089. $options = self::_checkOptions($options) + self::$_options;
  1090. // day expected but not parsed
  1091. if ((iconv_strpos($options['date_format'], 'd', 0, 'UTF-8') !== false) and (!isset($date['day']) or ($date['day'] === ""))) {
  1092. return false;
  1093. }
  1094. // month expected but not parsed
  1095. if ((iconv_strpos($options['date_format'], 'M', 0, 'UTF-8') !== false) and (!isset($date['month']) or ($date['month'] === ""))) {
  1096. return false;
  1097. }
  1098. // year expected but not parsed
  1099. if (((iconv_strpos($options['date_format'], 'Y', 0, 'UTF-8') !== false) or
  1100. (iconv_strpos($options['date_format'], 'y', 0, 'UTF-8') !== false)) and (!isset($date['year']) or ($date['year'] === ""))) {
  1101. return false;
  1102. }
  1103. // second expected but not parsed
  1104. if ((iconv_strpos($options['date_format'], 's', 0, 'UTF-8') !== false) and (!isset($date['second']) or ($date['second'] === ""))) {
  1105. return false;
  1106. }
  1107. // minute expected but not parsed
  1108. if ((iconv_strpos($options['date_format'], 'm', 0, 'UTF-8') !== false) and (!isset($date['minute']) or ($date['minute'] === ""))) {
  1109. return false;
  1110. }
  1111. // hour expected but not parsed
  1112. if (((iconv_strpos($options['date_format'], 'H', 0, 'UTF-8') !== false) or
  1113. (iconv_strpos($options['date_format'], 'h', 0, 'UTF-8') !== false)) and (!isset($date['hour']) or ($date['hour'] === ""))) {
  1114. return false;
  1115. }
  1116. return true;
  1117. }
  1118. /**
  1119. * Returns the default time format for $locale.
  1120. *
  1121. * @param string|Zend_Locale $locale OPTIONAL Locale of $number, possibly in string form (e.g. 'de_AT')
  1122. * @return string format
  1123. * @throws Zend_Locale_Exception
  1124. */
  1125. public static function getTimeFormat($locale = null)
  1126. {
  1127. $format = Zend_Locale_Data::getContent($locale, 'time');
  1128. if (empty($format)) {
  1129. require_once 'Zend/Locale/Exception.php';
  1130. throw new Zend_Locale_Exception("failed to receive data from locale $locale");
  1131. }
  1132. return $format;
  1133. }
  1134. /**
  1135. * Returns an array with 'hour', 'minute', and 'second' elements extracted from $time
  1136. * according to the order described in $format. For a format of 'H:i:s', and
  1137. * an input of 11:20:55, getTime() would return:
  1138. * array ('hour' => 11, 'minute' => 20, 'second' => 55)
  1139. * The optional $locale parameter may be used to help extract times from strings
  1140. * containing both a time and a day or month name.
  1141. *
  1142. * @param string $time Time string
  1143. * @param array $options Options: format_type, fix_date, locale, date_format. See {@link setOptions()} for details.
  1144. * @return array Possible array members: day, month, year, hour, minute, second, fixed, format
  1145. */
  1146. public static function getTime($time, array $options = array())
  1147. {
  1148. $options = self::_checkOptions($options) + self::$_options;
  1149. if (empty($options['date_format'])) {
  1150. $options['format_type'] = 'iso';
  1151. $options['date_format'] = self::getTimeFormat($options['locale']);
  1152. }
  1153. return self::_parseDate($time, $options);
  1154. }
  1155. /**
  1156. * Returns the default datetime format for $locale.
  1157. *
  1158. * @param string|Zend_Locale $locale OPTIONAL Locale of $number, possibly in string form (e.g. 'de_AT')
  1159. * @return string format
  1160. * @throws Zend_Locale_Exception
  1161. */
  1162. public static function getDateTimeFormat($locale = null)
  1163. {
  1164. $format = Zend_Locale_Data::getContent($locale, 'datetime');
  1165. if (empty($format)) {
  1166. require_once 'Zend/Locale/Exception.php';
  1167. throw new Zend_Locale_Exception("failed to receive data from locale $locale");
  1168. }
  1169. return $format;
  1170. }
  1171. /**
  1172. * Returns an array with 'year', 'month', 'day', 'hour', 'minute', and 'second' elements
  1173. * extracted from $datetime according to the order described in $format. For a format of 'd.M.y H:i:s',
  1174. * and an input of 10.05.1985 11:20:55, getDateTime() would return:
  1175. * array ('year' => 1985, 'month' => 5, 'day' => 10, 'hour' => 11, 'minute' => 20, 'second' => 55)
  1176. * The optional $locale parameter may be used to help extract times from strings
  1177. * containing both a time and a day or month name.
  1178. *
  1179. * @param string $datetime DateTime string
  1180. * @param array $options Options: format_type, fix_date, locale, date_format. See {@link setOptions()} for details.
  1181. * @return array Possible array members: day, month, year, hour, minute, second, fixed, format
  1182. */
  1183. public static function getDateTime($datetime, array $options = array())
  1184. {
  1185. $options = self::_checkOptions($options) + self::$_options;
  1186. if (empty($options['date_format'])) {
  1187. $options['format_type'] = 'iso';
  1188. $options['date_format'] = self::getDateTimeFormat($options['locale']);
  1189. }
  1190. return self::_parseDate($datetime, $options);
  1191. }
  1192. /**
  1193. * Internal method to detect of Unicode supports UTF8
  1194. * which should be enabled within vanilla php installations
  1195. *
  1196. * @return boolean
  1197. */
  1198. protected static function _getUniCodeSupport()
  1199. {
  1200. return (@preg_match('/\pL/u', 'a')) ? true : false;
  1201. }
  1202. }