Zend_Date-Basic.xml 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <sect1 id="zend.date.basic">
  2. <title>基本方法</title>
  3. <para>
  4. 下面的章节用例子来说明 <code>Zend_Date</code> 的基本用法。对于本手册,"dates" 总是暗示
  5. 带有时间的日历日期,即使没有显式说明,反之亦然。没有指定的部分就在内部缺省为 “零”。
  6. 这样,添加一个没有日历日期和一个时间值为 12 小时到另一个只包含日历日期的日期上,就变成那个被添加的日期的“中午”。
  7. </para>
  8. <para>
  9. 只设置一个指定的日期,没有时间部分,那么时间就设置为 00:00:00。相反,只设置一个指定的时间暗示在内部日期
  10. 设置为 01.01.1970 加上等同已经发生的小时,分钟和秒的秒数。一般来说,人们从开始点测量东西,如 A.D. 0年。
  11. 然而,许多软件系统使用1970年的第一秒作为开始点,并表示为时间戳偏移量。
  12. </para>
  13. <sect2 id="zend.date.basic.creation">
  14. <title>当前日期</title>
  15. <para>
  16. 不带任何参数,构造一个实例缺省地返回一个用PHP的 <code>time()</code>函数获得 <ulink url="http://en.wikipedia.org/wiki/Unix_Time">UNIX 时间戳</ulink>
  17. 当前本地日期的对象。请确认你的PHP环境有正确的 <link linkend="zend.date.setdefaulttimezone">缺省时区</link>。
  18. </para>
  19. <example id="zend.date.basic.creation.example-1">
  20. <title>生成当前日期</title>
  21. <programlisting role="php"><![CDATA[
  22. $date = new Zend_Date();
  23. // Output of the current timestamp
  24. print $date;
  25. ]]>
  26. </programlisting>
  27. </example>
  28. </sect2>
  29. <sect2 id="zend.date.basic.functions">
  30. <title>Zend_Date 的例子</title>
  31. <para>
  32. 对于不熟悉在其他语言或框架中的日期对象的开发者,回顾一下 <code>Zend_Date</code> 的基本方法是一个好的开始。
  33. 下面对每个方法提供了一个小的范例。
  34. </para>
  35. <sect3 id="zend.date.simple.functions.get">
  36. <title>输出日期</title>
  37. <para>
  38. 在 <code>Zend_Date</code> 对象里的日期可用 <code>get()</code> 方法获得为本地化的整数或字符串。
  39. 在后面的章节,我们将解释许多可用的选项。
  40. </para>
  41. <example id="zend.date.simple.functions.get.example-1">
  42. <title>get() - 输出日期</title>
  43. <programlisting role="php"><![CDATA[
  44. $date = new Zend_Date();
  45. // Output of the desired date
  46. print $date->get();
  47. ]]>
  48. </programlisting>
  49. </example>
  50. </sect3>
  51. <sect3 id="zend.date.simple.functions.set">
  52. <title>设置日期</title>
  53. <para>
  54. <code>set()</code> 方法修改存储在对象里的日期,并以时间戳(不是对象)的形式返回最终的日期值。
  55. 再强调一下, 在后面的章节,我们将解释许多可用的选项。
  56. </para>
  57. <example id="zend.date.simple.functions.set.example-1">
  58. <title>set() - 设置日期</title>
  59. <programlisting role="php"><![CDATA[
  60. $date = new Zend_Date();
  61. // Setting of a new time
  62. $date->set('13:00:00',Zend_Date::TIMES);
  63. print $date->get(Zend_Date::W3C);
  64. ]]>
  65. </programlisting>
  66. </example>
  67. </sect3>
  68. <sect3 id="zend.date.simple.functions.add">
  69. <title>加减日期</title>
  70. <para>
  71. 用 <code>add()</code> 来加两个日期常常是以时间的形式把一个真是的日期加到一个表示日期部分的假的时间戳,
  72. 如下面例子所示的12小时。<code>add()</code> 和 <code>sub()</code> 都使用相同的选项 <code>set()</code>,
  73. 我们将在后面解释。
  74. </para>
  75. <example id="zend.date.simple.functions.add.example-1">
  76. <title>add() - 加日期</title>
  77. <programlisting role="php"><![CDATA[
  78. $date = new Zend_Date();
  79. // changes $date by adding 12 hours
  80. $date->add('12:00:00', Zend_Date::TIMES);
  81. echo "Date via get() = ", $date->get(Zend_Date::W3C), "\n";
  82. // use magic __toString() method to call Zend_Date's toString()
  83. echo "Date via toString() = ", $date, "\n";
  84. ]]>
  85. </programlisting>
  86. </example>
  87. </sect3>
  88. <sect3 id="zend.date.simple.functions.compare">
  89. <title>比较日期</title>
  90. <para>
  91. 所有基本的 <code>Zend_Date</code> 方法都可以操作整个包含在对象里的日期,或者日期部分,如和一个绝对值比较分钟数。
  92. 例如使用 <code>compare()</code> 比较当前时间里的当前分钟可以和一个特定的分钟数,示例如下。
  93. </para>
  94. <example id="zend.date.simple.functions.compare.example-1">
  95. <title>compare() - 比较日期</title>
  96. <programlisting role="php"><![CDATA[
  97. $date = new Zend_Date();
  98. // Comparation of both times
  99. if ($date->compare(10, Zend_Date::MINUTE) == -1) {
  100. print "This hour is less than 10 minutes old";
  101. } else {
  102. print "This hour is at least 10 minutes old";
  103. }
  104. ]]>
  105. </programlisting>
  106. </example>
  107. <para>
  108. 对简单的相等比较,使用 <code>equals()</code>,它返回一个布尔值。
  109. </para>
  110. <example id="zend.date.simple.functions.compare.example-2">
  111. <title>equals() - 识别日期或日期部分</title>
  112. <programlisting role="php"><![CDATA[
  113. $date = new Zend_Date();
  114. // Comparation of the two dates
  115. if ($date->equals(10, Zend_Date::HOUR)) {
  116. print "It's 10 o'clock. Time to get to work.";
  117. } else {
  118. print "It is not 10 o'clock. You can keep sleeping.";
  119. }
  120. ]]>
  121. </programlisting>
  122. </example>
  123. </sect3>
  124. </sect2>
  125. </sect1>
  126. <!--
  127. vim:se ts=4 sw=4 et:
  128. -->