Zend_Locale-DatesTimes.xml 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  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(
  149. '13.04.2006',
  150. array(
  151. 'date_format' => 'dd.MM.yyyy',
  152. )
  153. );
  154. print_r($dateString); // outputs:
  155. Array
  156. (
  157. [date_format] => dd.MM.yyyy
  158. [locale] => de_DE
  159. [day] => 13
  160. [month] => 04
  161. [year] => 2006
  162. )
  163. // creates a Zend_Date object for this date
  164. $dateObject = new Zend_Date('13.04.2006', 'dd.MM.yyyy');
  165. print_r($dateObject); // outputs:
  166. Zend_Date Object
  167. (
  168. [_locale:Zend_Date:private] => de_DE
  169. [_fractional:Zend_Date:private] => 0
  170. [_precision:Zend_Date:private] => 3
  171. [_unixTimestamp:Zend_Date_DateObject:private] => 1144879200
  172. [_timezone:Zend_Date_DateObject:private] => Europe/Berlin
  173. [_offset:Zend_Date_DateObject:private] => -3600
  174. [_syncronised:Zend_Date_DateObject:private] => 0
  175. [_dst:protected] => 1
  176. )
  177. // alternatively, some types of problems with input data can be
  178. // automatically corrected
  179. $date = Zend_Locale_Format::getDate(
  180. '04.13.2006',
  181. array(
  182. 'date_format' => 'dd.MM.yyyy',
  183. 'fix_date' => true,
  184. )
  185. );
  186. print_r($date); // outputs:
  187. Array
  188. (
  189. [date_format] => dd.MM.yyyy
  190. [locale] => de_DE
  191. [day] => 13
  192. [month] => 04
  193. [year] => 2006
  194. [fixed] => 4
  195. )
  196. ]]></programlisting>
  197. </example>
  198. <para>
  199. Since <methodname>getDate()</methodname> is "locale-aware", specifying the
  200. <varname>$locale</varname> is sufficient for date strings adhering to that locale's
  201. format. The option '<property>fix_date</property>' uses simple tests to determine if the
  202. day or month is not valid, and then applies heuristics to try and correct any detected
  203. problems. Note the use of '<constant>Zend_Locale_Format::STANDARD</constant>' as the
  204. value for '<property>date_format</property>' to prevent the use of a class-wide default
  205. date format set using <methodname>setOptions()</methodname>. This forces getDate to use
  206. the default date format for <varname>$locale</varname>.
  207. </para>
  208. <example id="zend.locale.date.normalize.example-2">
  209. <title>Normalizing a date by locale</title>
  210. <programlisting language="php"><![CDATA[
  211. $locale = new Zend_Locale('de_AT');
  212. $date = Zend_Locale_Format::getDate('13.04.2006',
  213. array('date_format' =>
  214. Zend_Locale_Format::STANDARD,
  215. 'locale' => $locale)
  216. );
  217. print_r ($date);
  218. ]]></programlisting>
  219. </example>
  220. <para>
  221. A complete date and time is returned when the input contains both a date and time in the
  222. expected format.
  223. </para>
  224. <example id="zend.locale.date.normalize.example-3">
  225. <title>Normalizing a date with time</title>
  226. <programlisting language="php"><![CDATA[
  227. $locale = new Zend_Locale('de_AT');
  228. $date = Zend_Locale_Format::getDate('13.04.2005 22:14:55',
  229. array('date_format' =>
  230. Zend_Locale_Format::STANDARD,
  231. 'locale' => $locale)
  232. );
  233. print_r ($date);
  234. ]]></programlisting>
  235. </example>
  236. <para>
  237. If a specific format is desired, specify the <varname>$format</varname> argument,
  238. without giving a <varname>$locale</varname>. Only single-letter codes
  239. (H, m, s, y, M, d), and MMMM and EEEE are supported in the <varname>$format</varname>.
  240. </para>
  241. <example id="zend.locale.date.normalize.example-4">
  242. <title>Normalizing a userdefined date</title>
  243. <programlisting language="php"><![CDATA[
  244. $date = Zend_Locale_Format::getDate('13200504T551422',
  245. array('date_format' =>
  246. 'ddyyyyMM ssmmHH')
  247. );
  248. print_r ($date);
  249. ]]></programlisting>
  250. </example>
  251. <para>
  252. The format can include the following signs :
  253. </para>
  254. <table id="zend.locale.date.normalize.table-3">
  255. <title>Format definition</title>
  256. <tgroup cols='2'>
  257. <thead>
  258. <row>
  259. <entry>Format Letter</entry>
  260. <entry>Description</entry>
  261. </row>
  262. </thead>
  263. <tbody>
  264. <row>
  265. <entry>d or dd</entry>
  266. <entry>1 or 2 digit day</entry>
  267. </row>
  268. <row>
  269. <entry>M or MM</entry>
  270. <entry>1 or 2 digit month</entry>
  271. </row>
  272. <row>
  273. <entry>y or yy</entry>
  274. <entry>1 or 2 digit year</entry>
  275. </row>
  276. <row>
  277. <entry>yyyy</entry>
  278. <entry>4 digit year</entry>
  279. </row>
  280. <row>
  281. <entry>h</entry>
  282. <entry>1 or 2 digit hour</entry>
  283. </row>
  284. <row>
  285. <entry>m</entry>
  286. <entry>1 or 2 digit minute</entry>
  287. </row>
  288. <row>
  289. <entry>s</entry>
  290. <entry>1 or 2 digit second</entry>
  291. </row>
  292. </tbody>
  293. </tgroup>
  294. </table>
  295. <para>
  296. Examples for proper formats are
  297. </para>
  298. <table id="zend.locale.date.normalize.table-4">
  299. <title>Example formats</title>
  300. <tgroup cols='3'>
  301. <thead>
  302. <row>
  303. <entry>Formats</entry>
  304. <entry>Input</entry>
  305. <entry>Output</entry>
  306. </row>
  307. </thead>
  308. <tbody>
  309. <row>
  310. <entry>dd.MM.yy</entry>
  311. <entry>1.4.6</entry>
  312. <entry>['day'] => 1, ['month'] => 4, ['year'] => 6</entry>
  313. </row>
  314. <row>
  315. <entry>dd.MM.yy</entry>
  316. <entry>01.04.2006</entry>
  317. <entry>['day'] => 1, ['month'] => 4, ['year'] => 2006</entry>
  318. </row>
  319. <row>
  320. <entry>yyyyMMdd</entry>
  321. <entry>1.4.6</entry>
  322. <entry>['day'] => 6, ['month'] => 4, ['year'] => 1</entry>
  323. </row>
  324. </tbody>
  325. </tgroup>
  326. </table>
  327. <note>
  328. <title>Database date format</title>
  329. <para>
  330. To parse a database date value (f.e. MySql or MsSql), use
  331. <classname>Zend_Date</classname>'s ISO_8601 format instead of getDate().
  332. </para>
  333. </note>
  334. <para>
  335. The option '<property>fix_date</property>' uses simple tests to determine if the day or
  336. month is not valid, and then applies heuristics to try and correct any detected
  337. problems. <methodname>getDate()</methodname> automatically detects and corrects some
  338. kinds of problems with input, such as misplacing the year:
  339. </para>
  340. <example id="zend.locale.date.normalize.example-5">
  341. <title>Automatic correction of input dates</title>
  342. <programlisting language="php"><![CDATA[
  343. $date = Zend_Locale_Format::getDate('41.10.20',
  344. array('date_format' => 'ddMMyy',
  345. 'fix_date' => true)
  346. );
  347. // instead of 41 for the day, the 41 will be returned as year value
  348. print_r ($date);
  349. ]]></programlisting>
  350. </example>
  351. </sect2>
  352. <sect2 id="zend.locale.date.test">
  353. <title>Testing Dates</title>
  354. <para>
  355. Use <methodname>checkDateFormat($inputString, array('date_format' => $format,
  356. $locale))</methodname> to check if a given string contains all expected date parts.
  357. The <methodname>checkDateFormat()</methodname> method uses
  358. <methodname>getDate()</methodname>, but without the option
  359. '<property>fixdate</property>' to avoid returning <constant>TRUE</constant> when the
  360. input fails to conform to the date format. If errors are detected in the input, such as
  361. swapped values for months and days, the option '<property>fixdate</property>' method
  362. will apply heuristics to "correct" dates before determining their validity.
  363. </para>
  364. <example id="zend.locale.date.test.example-1">
  365. <title>Date testing</title>
  366. <programlisting language="php"><![CDATA[
  367. $locale = new Zend_Locale('de_AT');
  368. // using the default date format for 'de_AT', is this a valid date?
  369. if (Zend_Locale_Format::checkDateFormat('13.Apr.2006',
  370. array('date_format' =>
  371. Zend_Locale_Format::STANDARD,
  372. $locale)
  373. ) {
  374. print "date";
  375. } else {
  376. print "not a date";
  377. }
  378. ]]></programlisting>
  379. </example>
  380. </sect2>
  381. <sect2 id="zend.locale.time.normalizing">
  382. <title>Normalizing a Time</title>
  383. <para>
  384. Normally, a time will be returned with a date, if the input contains both. If the proper
  385. format is not known, but the locale relevant to the user input is known, then
  386. <methodname>getTime()</methodname> should be used, because it uses the default time
  387. format for the selected locale.
  388. </para>
  389. <example id="zend.locale.time.normalizing.example-1">
  390. <title>Normalize an unknown time</title>
  391. <programlisting language="php"><![CDATA[
  392. $locale = new Zend_Locale('de_AT');
  393. if (Zend_Locale_Format::getTime('13:44:42',
  394. array('date_format' =>
  395. Zend_Locale_Format::STANDARD,
  396. 'locale' => $locale)) {
  397. print "time";
  398. } else {
  399. print "not a time";
  400. }
  401. ]]></programlisting>
  402. </example>
  403. </sect2>
  404. <sect2 id="zend.locale.time.test">
  405. <title>Testing Times</title>
  406. <para>
  407. Use <methodname>checkDateFormat()</methodname> to check if a given string contains a
  408. proper time. The usage is exact the same as with checking Dates, only
  409. <property>date_format</property> should contain the parts which you expect to have.
  410. </para>
  411. <example id="zend.locale.time.test.example-1">
  412. <title>Testing a time</title>
  413. <programlisting language="php"><![CDATA[
  414. $locale = new Zend_Locale('de_AT');
  415. if (Zend_Locale_Format::checkDateFormat('13:44:42',
  416. array('date_format' => 'HH:mm:ss',
  417. 'locale' => $locale)) {
  418. print "time";
  419. } else {
  420. print "not a time";
  421. }
  422. ]]></programlisting>
  423. </example>
  424. </sect2>
  425. </sect1>
  426. <!--
  427. vim:se ts=4 sw=4 et:
  428. -->