Zend_Date-Additional.xml 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. <sect1 id="zend.date.additional">
  2. <title>Working 范例 </title>
  3. <para>
  4. 在本章内,我们将描述若干附加的函数,它们在 <code>Zend_Date</code> 中也有效。
  5. 当然所述的函数有另外的范例来示范期望的工作和简单的如何正确使用的 API 。
  6. </para>
  7. <sect2 id="zend.date.additional.checking">
  8. <title> 检查日期 </title>
  9. <para>
  10. 大部分输入的日期可能是字符串,问题是你不能确保这些字符串是真的日期。因此 <code>Zend_Date</code>
  11. 有个特有的静态函数来加查日期字符串。<code>Zend_Locale</code> 有个特有的函数 <code>getDate($date, $locale);</code> ,它解析
  12. 日期并返回正确的格式化的日期部分。例如月名将被识别和返回月数。
  13. 但 <code>Zend_Locale</code> 不知道什么是日期,因为它是个格式化和本地化的类,所以我们集成了一个特有的函数 <code>isDate($date);</code> 来做这个。
  14. </para>
  15. <para>
  16. <code>isDate($date, $format, $locale);</code> 最多可以带 3 个参数但最少需要一个参数。所以我们需要校验的日期当然是它本身是否一个字符串。
  17. 第二个参数是日期的格式。如果没有给定格式,使用地方的标准格式。关于格式的细节参见 <link linkend="zend.date.constants.selfdefinedformats">自定义格式</link> 。
  18. </para>
  19. <para>
  20. 第三个参数也是可选的并可用于给定一个地方。我们需要地方来格式化月名和天名(monthnames and daynames)。
  21. 所以用第三个参数我们可以根据给定的地方识别 '01.Jänner.2000' 或 '01.January.2000'这样的日期。
  22. </para>
  23. <para>
  24. <code>isDate();</code> 当然来检查是否一个日期存在。<code>Zend_Date</code>自己不检查日期。
  25. 所以有可能用 <code>Zend_Date</code> 生成一个如 '31.February.2000'的日期,因为 <code>Zend_Date</code> 将
  26. 自动纠正日期并返回适当的日期,在这个例子中为 '03.March.2000' 。而 <code>isDate()</code> 将检查并对 '31.February.2000' 返回 false,
  27. 因为它知道这是个不可能的日期。
  28. </para>
  29. <example id="zend.date.additional.checking.example-1">
  30. <title>检查日期</title>
  31. <programlisting role="php"><![CDATA[
  32. // Checking dates
  33. $date = '01.03.2000';
  34. if (Zend_Date::isDate($date)) {
  35. print "String $date is a date";
  36. } else {
  37. print "String $date is NO date";
  38. }
  39. // Checking localized dates
  40. $date = '01 February 2000';
  41. if (Zend_Date::isDate($date,'dd MMMM yyyy', 'en')) {
  42. print "String $date is a date";
  43. } else {
  44. print "String $date is NO date";
  45. }
  46. // Checking impossible dates
  47. $date = '30 February 2000';
  48. if (Zend_Date::isDate($date,'dd MMMM yyyy', 'en')) {
  49. print "String $date is a date";
  50. } else {
  51. print "String $date is NO date";
  52. }
  53. ]]>
  54. </programlisting>
  55. </example>
  56. </sect2>
  57. <sect2 id="zend.date.additional.sunrise-sunset">
  58. <title>日出和日落</title>
  59. <para>
  60. <code>Zend_Date</code> 也集成了从太阳获得信息的功能。通常在一个特定的日子里获得日出和日落的时间是有必要的。
  61. 用<code>Zend_Date</code> 来做很容易,因为仅仅给定位置就可以计算到日出日落的时间。
  62. </para>
  63. <para>
  64. 正如很多人不知道他们所居住的城市的位置,我们也使用助手类来提供了世界上大约 250个首都和其它大城市的位置数据。
  65. 大多数人使用他们附近的城市来作为地方,这样计算上会差一点时间。
  66. </para>
  67. <para>
  68. 用 <code>Zend_Date_Cities::getCityList</code> 可以生成一个列表来选择城市。它返回所有在助手类中预定义的可用的城市名称。
  69. </para>
  70. <example id="zend.date.additional.sunrise-sunset.example-1">
  71. <title> 获得所有可用的城市 </title>
  72. <programlisting role="php"><![CDATA[
  73. // Output the complete list of available cities
  74. print_r (Zend_Date_Cities::getCityList());
  75. ]]>
  76. </programlisting>
  77. </example>
  78. <para>
  79. 用 <code>Zend_Date_Cities::City()</code> 函数可以获得位置。它接受由 <code>Zend_Date_Cities::getCityList()</code> 函数返回的城市名
  80. 和可选的第二个参数来设置地平线(horizon).
  81. </para>
  82. <para>
  83. '<code>effective</code>' horizon is used.
  84. 有 4 个已定义的地平线可用于位置类精确计算日落和日出。'<code>horizon</code>' 参数在所有的函数中总是可选的。
  85. 如果没有指定,则使用 '<code>effective</code>' 。
  86. </para>
  87. <table id="zend.date.additional.sunrise-sunset.table">
  88. <title> 日落和日出支持的地平线(horizons)类型 </title>
  89. <tgroup cols="3">
  90. <thead>
  91. <row>
  92. <entry>地平线(Horizon)</entry>
  93. <entry>描述</entry>
  94. <entry>用法</entry>
  95. </row>
  96. </thead>
  97. <tbody>
  98. <row>
  99. <entry>effective</entry>
  100. <entry> 标准地平线 </entry>
  101. <entry> 认为世界是一个球。这个地平线是缺省值 </entry>
  102. </row>
  103. <row>
  104. <entry>civil</entry>
  105. <entry>通用地平线</entry>
  106. <entry>常用于媒体如电视和电台</entry>
  107. </row>
  108. <row>
  109. <entry>nautic</entry>
  110. <entry>水兵地平线</entry>
  111. <entry>常用于大海导航</entry>
  112. </row>
  113. <row>
  114. <entry>astronomic</entry>
  115. <entry>天文学地平线</entry>
  116. <entry>常用于星际计算</entry>
  117. </row>
  118. </tbody>
  119. </tgroup>
  120. </table>
  121. <para>
  122. 当然也可以给出和计算自定义位置,因此 '<code>latitude</code>' 和 '<code>longitude</code>' 必需给定,而 '<code>horizon</code>' 是可选的。
  123. </para>
  124. <example id="zend.date.additional.sunrise-sunset.example-2">
  125. <title>获得一个城市的位置</title>
  126. <programlisting role="php"><![CDATA[
  127. // Get the location for a defined city
  128. // uses the effective horizon as no horizon is defined
  129. print_r (Zend_Date_Cities::City('Vienna'));
  130. // use the nautic horizon
  131. print_r (Zend_Date_Cities::City('Vienna', 'nautic'));
  132. // self definition of a location
  133. $mylocation = array('latitude' => 41.5, 'longitude' => 13.2446);
  134. ]]>
  135. </programlisting>
  136. </example>
  137. <para>
  138. 现在所有需要的数据可以被设置了,下一步是创建带有计算日落和日出的日期的 <code>Zend_Date</code> 对象。
  139. 有三个函数用于计算,用 '<code>getSunset()</code>' 计算日落,用 '<code>getSunrise()</code>' 计算日出,所有和太阳相关的信息使用 '<code>getSunInfo()</code>' 。
  140. 经过计算后,<code>Zend_Date</code> 对象返回时就带有计算过的时间。
  141. </para>
  142. <example id="zend.date.additional.sunrise-sunset.example-3">
  143. <title> 计算太阳信息 </title>
  144. <programlisting role="php"><![CDATA[
  145. // Get the location for a defined city
  146. $city = Zend_Date_Cities::City('Vienna');
  147. // create a date object for the day for which the sun has to be calculated
  148. $date = new Zend_Date('10.03.2007', Zend_Date::ISO_8601, 'de');
  149. // calculate sunset
  150. $sunset = $date->getSunset($city);
  151. print $sunset->get(Zend_Date::ISO_8601);
  152. // calculate all sun informations
  153. $info = $date->getSunInfo($city);
  154. foreach ($info as $sun) {
  155. print "\n" . $sun->get(Zend_Date::ISO_8601);
  156. }
  157. ]]>
  158. </programlisting>
  159. </example>
  160. </sect2>
  161. <sect2 id="zend.date.additional.timezones">
  162. <title>时区</title>
  163. <para>
  164. 时区和日期一样重要,根据用户所居住的地方,有若干时区,所以使用日期意味着要正确设置时区。
  165. 这听起来很复杂但其实比较简单。正如在 <code>Zend_Date</code> 的第一章所提到的,缺省的时区已经通过 <code>php.ini</code> 或在引导文件(bootstrap file)里定义来设置,
  166. </para>
  167. <para>
  168. <code>Zend_Date</code> 对象当然也存储实际的时区。即使在对象创建后修改时区,它也能记住原来的时区并使用它。
  169. 也不必要在使用php函数的代码里修改时区,<code>Zend_Date</code> 有两个内置的函数来处理这个。
  170. </para>
  171. <para>
  172. <code>getTimezone()</code> 返回实际的在 <code>Zend_Date</code> 对象里的时区。请记住 <code>Zend_Date</code> 在内部不和 php 耦合,
  173. 返回的时区不是 php 脚本的时区二是对象的时区。
  174. <code>setTimezone($zone)</code> 是第二个函数并为 <code>Zend_Date</code> 设置新时区。给定的时区总是被检查的,如果不存在就抛出异常。
  175. 另外通过不带区域参数调用 <code>setTimezone()</code> 实际脚本或系统时区可以被设置到日期对象,在对象被创建是这个也会自动完成。
  176. </para>
  177. <example id="zend.date.additional.timezones.example-1">
  178. <title> 使用时区 </title>
  179. <programlisting role="php"><![CDATA[
  180. // Set a default timezone... this has to be done within the bootstrap
  181. // file or php.ini
  182. // We do this here just for having a complete example
  183. date_default_timezone_set('Europe/Vienna');
  184. // create a date object
  185. $date = new Zend_Date('10.03.2007', Zend_Date::DATES, 'de');
  186. // view our date object
  187. print $date->getIso();
  188. // what timezone do we have ?
  189. print $date->getTimezone();
  190. // set another timezone
  191. $date->setTimezone('America/Chicago');
  192. // what timezone do we now have ?
  193. print $date->getTimezone();
  194. // see the changed date object
  195. print $date->getIso();
  196. ]]>
  197. </programlisting>
  198. </example>
  199. <para>
  200. 如上面例子的第一行所示,<code>Zend_Date</code> 对创建对象总是使用实际的时区。在被创建的对象里修改时区
  201. 对日期自己有影响。日期总是和时区相关。对 <code>Zend_Date</code> 对象修改时区不会修改 <code>Zend_Date</code> 的时间。
  202. 请记住在内部日期总是以时间戳和 GMT 时区存储。所以时区就意味者对用户所在时区和地区对实际的国际时间的加减。
  203. </para>
  204. <para>
  205. 在 <code>Zend_Date</code> 里耦合时区有另一个正面影响,在不同的时区有可能有若干个日期。
  206. </para>
  207. <example id="zend.date.additional.timezones.example-2">
  208. <title> 多重时区 </title>
  209. <programlisting role="php"><![CDATA[
  210. // Set a default timezone... this has to be done within the bootstrap
  211. // file or php.ini.
  212. // We do this here just for having a complete example.
  213. date_default_timezone_set('Europe/Vienna');
  214. // create a date object
  215. $date = new Zend_Date('10.03.2007 00:00:00', Zend_Date::ISO_8601, 'de');
  216. // view our date object
  217. print $date->getIso();
  218. // the date stays unchanged even after changeing the timezone
  219. date_default_timezone_set('America/Chicago');
  220. print $date->getIso();
  221. $otherdate = clone $date;
  222. $otherdate->setTimezone('Brazil/Acre');
  223. // view our date object
  224. print $otherdate->getIso();
  225. // set the object to the actual systems timezone
  226. $lastdate = clone $date;
  227. $lastdate->setTimezone();
  228. // view our date object
  229. print $lastdate->getIso();
  230. ]]>
  231. </programlisting>
  232. </example>
  233. </sect2>
  234. </sect1>
  235. <!--
  236. vim:se ts=4 sw=4 et:
  237. -->