Zend_Locale-DatesTimes.xml 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="zend.locale.date.datesandtimes">
  4. <title>Working with Dates and Times</title>
  5. <para>
  6. <classname>Zend_Locale_Format</classname> provides several methods for working with dates and times to help convert and
  7. normalize between different formats for different locales. Use <classname>Zend_Date</classname> for manipulating dates,
  8. and working with date strings that already conform to
  9. <link linkend="zend.date.constants">one of the many internationally recognized standard formats, or one of the localized date formats supported by <classname>Zend_Date</classname>
  10. </link>
  11. . Using an existing, pre-defined format offers advantages, including the use of well-tested code, and the
  12. assurance of some degree of portability and interoperability (depending on the standard used). The examples
  13. below do not follow these recommendations, since using non-standard date formats would needlessly increase the
  14. difficulty of understanding these examples.
  15. </para>
  16. <sect2 id="zend.locale.date.normalize">
  17. <title>Normalizing Dates and Times</title>
  18. <para>
  19. The <methodname>getDate()</methodname> method parses strings containing dates in localized formats. The results are
  20. returned in a structured array, with well-defined keys for each part of the date. In addition, the array
  21. will contain a key 'date_format' showing the format string used to parse the input date string. Since a
  22. localized date string may not contain all parts of a date/time, the key-value pairs are optional. For
  23. example, if only the year, month, and day is given, then all time values are suppressed from the returned
  24. array, and vice-versa if only hour, minute, and second were given as input. If no date or time can be found
  25. within the given input, an exception will be thrown.
  26. </para>
  27. <para>
  28. If <methodname>setOption(array('fix_date' => true))</methodname> is set the <methodname>getDate()</methodname> method adds a key 'fixed'
  29. with a whole number value indicating if the input date string required "fixing" by rearranging the day,
  30. month, or year in the input to fit the format used.
  31. </para>
  32. <table id="zend.locale.date.normalize.table-1">
  33. <title>Key values for getDate() with option 'fix_date'</title>
  34. <tgroup cols='2'>
  35. <thead>
  36. <row>
  37. <entry>value</entry>
  38. <entry>meaning</entry>
  39. </row>
  40. </thead>
  41. <tbody>
  42. <row>
  43. <entry>0</entry>
  44. <entry>nothing to fix</entry>
  45. </row>
  46. <row>
  47. <entry>1</entry>
  48. <entry>fixed false month</entry>
  49. </row>
  50. <row>
  51. <entry>2</entry>
  52. <entry>swapped day and year</entry>
  53. </row>
  54. <row>
  55. <entry>3</entry>
  56. <entry>swapped month and year</entry>
  57. </row>
  58. <row>
  59. <entry>4</entry>
  60. <entry>swapped month and day</entry>
  61. </row>
  62. </tbody>
  63. </tgroup>
  64. </table>
  65. <para>
  66. For those needing to specify explicitly the format of the date string, the following format token specifiers
  67. are supported. If an invalid format specifier is used, such as the <acronym>PHP</acronym> 'i' specifier when in <acronym>ISO</acronym> format
  68. mode, then an error will be thrown by the methods in <classname>Zend_Locale_Format</classname> that support user-defined formats.
  69. </para>
  70. <para>
  71. These specifiers (below) are a small subset of the full "ISO" set supported by <classname>Zend_Date</classname>'s
  72. <methodname>toString()</methodname>. If you need to use <acronym>PHP</acronym> <methodname>date()</methodname> compatible format specifiers, then first
  73. call <methodname>setOptions(array('format_type' => 'php'))</methodname>. And if you want to convert only one special format
  74. string from <acronym>PHP</acronym> <methodname>date()</methodname> compatible format to "ISO" format use <methodname>convertPhpToIsoFormat()</methodname>.
  75. Currently, the only practical difference relates to the specifier for minutes ('m' using the <acronym>ISO</acronym> default,
  76. and 'i' using the <acronym>PHP</acronym> date format).
  77. </para>
  78. <table id="zend.locale.date.normalize.table-2">
  79. <title>Return values</title>
  80. <tgroup cols='5'>
  81. <thead>
  82. <row>
  83. <entry>getDate() format character</entry>
  84. <entry>Array key</entry>
  85. <entry>Returned value</entry>
  86. <entry>Minimum</entry>
  87. <entry>Maximum</entry>
  88. </row>
  89. </thead>
  90. <tbody>
  91. <row>
  92. <entry>d</entry>
  93. <entry>day</entry>
  94. <entry>integer</entry>
  95. <entry>1</entry>
  96. <entry>31</entry>
  97. </row>
  98. <row>
  99. <entry>M</entry>
  100. <entry>month</entry>
  101. <entry>integer</entry>
  102. <entry>1</entry>
  103. <entry>12</entry>
  104. </row>
  105. <row>
  106. <entry>y</entry>
  107. <entry>year</entry>
  108. <entry>integer</entry>
  109. <entry>no limit</entry>
  110. <entry>PHP integer's maximum</entry>
  111. </row>
  112. <row>
  113. <entry>h</entry>
  114. <entry>hour</entry>
  115. <entry>integer</entry>
  116. <entry>0</entry>
  117. <entry>PHP integer's maximum</entry>
  118. </row>
  119. <row>
  120. <entry>m</entry>
  121. <entry>minute</entry>
  122. <entry>integer</entry>
  123. <entry>0</entry>
  124. <entry>PHP integer's maximum</entry>
  125. </row>
  126. <row>
  127. <entry>s</entry>
  128. <entry>second</entry>
  129. <entry>integer</entry>
  130. <entry>0</entry>
  131. <entry>PHP integer's maximum</entry>
  132. </row>
  133. </tbody>
  134. </tgroup>
  135. </table>
  136. <example id="zend.locale.date.normalize.example-1">
  137. <title>Normalizing a date</title>
  138. <programlisting language="php"><![CDATA[
  139. $dateString = Zend_Locale_Format::getDate('13.04.2006',
  140. array('date_format' =>
  141. 'dd.MM.yyyy')
  142. );
  143. // creates a Zend_Date object for this date
  144. $dateObject = Zend_Date('13.04.2006',
  145. array('date_format' => 'dd.MM.yyyy'));
  146. print_r($dateString); // outputs:
  147. Array
  148. (
  149. [format] => dd.MM.yyyy
  150. [day] => 13
  151. [month] => 4
  152. [year] => 2006
  153. )
  154. // alternatively, some types of problems with input data can be
  155. // automatically corrected
  156. $date = Zend_Locale_Format::getDate('04.13.2006',
  157. array('date_format' => 'dd.MM.yyyy',
  158. 'fix_date' => true)
  159. );
  160. print_r($date); // outputs:
  161. Array
  162. (
  163. [format] => dd.MM.yyyy
  164. [day] => 13
  165. [month] => 4
  166. [year] => 2006
  167. [fixed] => 4
  168. )
  169. ]]></programlisting>
  170. </example>
  171. <para>
  172. Since <methodname>getDate()</methodname> is "locale-aware", specifying the <varname>$locale</varname> is sufficient for date
  173. strings adhering to that locale's format. The option '<code>fix_date</code>' uses simple tests to
  174. determine if the day or month is not valid, and then applies heuristics to try and correct any detected
  175. problems. Note the use of '<constant>Zend_Locale_Format::STANDARD</constant>' as the value for
  176. '<code>date_format</code>' to prevent the use of a class-wide default date format set using
  177. <methodname>setOptions()</methodname>. This forces getDate to use the default date format for <varname>$locale</varname>.
  178. </para>
  179. <example id="zend.locale.date.normalize.example-2">
  180. <title>Normalizing a date by locale</title>
  181. <programlisting language="php"><![CDATA[
  182. $locale = new Zend_Locale('de_AT');
  183. $date = Zend_Locale_Format::getDate('13.04.2006',
  184. array('date_format' =>
  185. Zend_Locale_Format::STANDARD,
  186. 'locale' => $locale)
  187. );
  188. print_r ($date);
  189. ]]></programlisting>
  190. </example>
  191. <para>
  192. A complete date and time is returned when the input contains both a date and time in the expected format.
  193. </para>
  194. <example id="zend.locale.date.normalize.example-3">
  195. <title>Normalizing a date with time</title>
  196. <programlisting language="php"><![CDATA[
  197. $locale = new Zend_Locale('de_AT');
  198. $date = Zend_Locale_Format::getDate('13.04.2005 22:14:55',
  199. array('date_format' =>
  200. Zend_Locale_Format::STANDARD,
  201. 'locale' => $locale)
  202. );
  203. print_r ($date);
  204. ]]></programlisting>
  205. </example>
  206. <para>
  207. If a specific format is desired, specify the <varname>$format</varname> argument, without giving a
  208. <varname>$locale</varname>. Only single-letter codes (H, m, s, y, M, d), and MMMM and EEEE are supported in the
  209. <varname>$format</varname>.
  210. </para>
  211. <example id="zend.locale.date.normalize.example-4">
  212. <title>Normalizing a userdefined date</title>
  213. <programlisting language="php"><![CDATA[
  214. $date = Zend_Locale_Format::getDate('13200504T551422',
  215. array('date_format' =>
  216. 'ddyyyyMM ssmmHH')
  217. );
  218. print_r ($date);
  219. ]]></programlisting>
  220. </example>
  221. <para>
  222. The format can include the following signs :
  223. </para>
  224. <table id="zend.locale.date.normalize.table-3">
  225. <title>Format definition</title>
  226. <tgroup cols='2'>
  227. <thead>
  228. <row>
  229. <entry>Format Letter</entry>
  230. <entry>Description</entry>
  231. </row>
  232. </thead>
  233. <tbody>
  234. <row>
  235. <entry>d or dd</entry>
  236. <entry>1 or 2 digit day</entry>
  237. </row>
  238. <row>
  239. <entry>M or MM</entry>
  240. <entry>1 or 2 digit month</entry>
  241. </row>
  242. <row>
  243. <entry>y or yy</entry>
  244. <entry>1 or 2 digit year</entry>
  245. </row>
  246. <row>
  247. <entry>yyyy</entry>
  248. <entry>4 digit year</entry>
  249. </row>
  250. <row>
  251. <entry>h</entry>
  252. <entry>1 or 2 digit hour</entry>
  253. </row>
  254. <row>
  255. <entry>m</entry>
  256. <entry>1 or 2 digit minute</entry>
  257. </row>
  258. <row>
  259. <entry>s</entry>
  260. <entry>1 or 2 digit second</entry>
  261. </row>
  262. </tbody>
  263. </tgroup>
  264. </table>
  265. <para>
  266. Examples for proper formats are
  267. </para>
  268. <table id="zend.locale.date.normalize.table-4">
  269. <title>Example formats</title>
  270. <tgroup cols='3'>
  271. <thead>
  272. <row>
  273. <entry>Formats</entry>
  274. <entry>Input</entry>
  275. <entry>Output</entry>
  276. </row>
  277. </thead>
  278. <tbody>
  279. <row>
  280. <entry>dd.MM.yy</entry>
  281. <entry>1.4.6</entry>
  282. <entry>['day'] => 1,
  283. ['month'] => 4,
  284. ['year'] => 6</entry>
  285. </row>
  286. <row>
  287. <entry>dd.MM.yy</entry>
  288. <entry>01.04.2006</entry>
  289. <entry>['day'] => 1, ['month'] => 4, ['year'] => 2006</entry>
  290. </row>
  291. <row>
  292. <entry>yyyyMMdd</entry>
  293. <entry>1.4.6</entry>
  294. <entry>['day'] => 6, ['month'] => 4, ['year'] => 1</entry>
  295. </row>
  296. </tbody>
  297. </tgroup>
  298. </table>
  299. <note>
  300. <title>Database date format</title>
  301. <para>
  302. To parse a database date value (f.e. MySql or MsSql), use <classname>Zend_Date</classname>'s ISO_8601 format instead of
  303. getDate().
  304. </para>
  305. </note>
  306. <para>
  307. The option '<code>fix_date</code>' uses simple tests to determine if the day or month is not
  308. valid, and then applies heuristics to try and correct any detected problems. <methodname>getDate()</methodname>
  309. automatically detects and corrects some kinds of problems with input, such as misplacing the year:
  310. </para>
  311. <example id="zend.locale.date.normalize.example-5">
  312. <title>Automatic correction of input dates</title>
  313. <programlisting language="php"><![CDATA[
  314. $date = Zend_Locale_Format::getDate('41.10.20',
  315. array('date_format' => 'ddMMyy',
  316. 'fix_date' => true)
  317. );
  318. // instead of 41 for the day, the 41 will be returned as year value
  319. print_r ($date);
  320. ]]></programlisting>
  321. </example>
  322. </sect2>
  323. <sect2 id="zend.locale.date.test">
  324. <title>Testing Dates</title>
  325. <para>
  326. Use <methodname>checkDateFormat($inputString, array('date_format' => $format,
  327. $locale))</methodname> to check if a given string contains all expected date parts.
  328. The <methodname>checkDateFormat()</methodname> method uses
  329. <methodname>getDate()</methodname>, but without the option <code>'fixdate'</code> to
  330. avoid returning <constant>TRUE</constant> when the input fails to conform to the date
  331. format. If errors are detected in the input, such as swapped values for months and days,
  332. the option <code>'fixdate'</code> method will apply heuristics to "correct" dates before
  333. determining their validity.
  334. </para>
  335. <example id="zend.locale.date.test.example-1">
  336. <title>Date testing</title>
  337. <programlisting language="php"><![CDATA[
  338. $locale = new Zend_Locale('de_AT');
  339. // using the default date format for 'de_AT', is this a valid date?
  340. if (Zend_Locale_Format::checkDateFormat('13.Apr.2006',
  341. array('date_format' =>
  342. Zend_Locale_Format::STANDARD,
  343. $locale)
  344. ) {
  345. print "date";
  346. } else {
  347. print "not a date";
  348. }
  349. ]]></programlisting>
  350. </example>
  351. </sect2>
  352. <sect2 id="zend.locale.time.normalizing">
  353. <title>Normalizing a Time</title>
  354. <para>
  355. Normally, a time will be returned with a date, if the input contains both. If the proper format is not
  356. known, but the locale relevant to the user input is known, then <methodname>getTime()</methodname> should be used,
  357. because it uses the default time format for the selected locale.
  358. </para>
  359. <example id="zend.locale.time.normalizing.example-1">
  360. <title>Normalize an unknown time</title>
  361. <programlisting language="php"><![CDATA[
  362. $locale = new Zend_Locale('de_AT');
  363. if (Zend_Locale_Format::getTime('13:44:42',
  364. array('date_format' =>
  365. Zend_Locale_Format::STANDARD,
  366. 'locale' => $locale)) {
  367. print "time";
  368. } else {
  369. print "not a time";
  370. }
  371. ]]></programlisting>
  372. </example>
  373. </sect2>
  374. <sect2 id="zend.locale.time.test">
  375. <title>Testing Times</title>
  376. <para>
  377. Use <methodname>checkDateFormat()</methodname> to check if a given string contains a proper time.
  378. The usage is exact the same as with checking Dates, only <code>date_format</code>
  379. should contain the parts which you expect to have.
  380. </para>
  381. <example id="zend.locale.time.test.example-1">
  382. <title>Testing a time</title>
  383. <programlisting language="php"><![CDATA[
  384. $locale = new Zend_Locale('de_AT');
  385. if (Zend_Locale_Format::checkDateFormat('13:44:42',
  386. array('date_format' => 'HH:mm:ss',
  387. 'locale' => $locale)) {
  388. print "time";
  389. } else {
  390. print "not a time";
  391. }
  392. ]]></programlisting>
  393. </example>
  394. </sect2>
  395. </sect1>
  396. <!--
  397. vim:se ts=4 sw=4 et:
  398. -->