Zend_Locale-DatesTimes.xml 18 KB

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