2
0

Zend_Date-Basic.xml 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="zend.date.basic">
  4. <title>Basic Methods</title>
  5. <para>
  6. The following sections show basic usage of <classname>Zend_Date</classname> primarily by
  7. example. For this manual, "dates" always imply a calendar date with a time, even when not
  8. explicitly mentioned, and vice-versa. The part not specified defaults to an internal
  9. representation of "zero". Thus, adding a date having no calendar date and a time value of 12
  10. hours to another date consisting only of a calendar date would result in a date having that
  11. calendar date and a time of "noon".
  12. </para>
  13. <para>
  14. Setting only a specific date, with no time part, implies a time set to 00:00:00. Conversely,
  15. setting only a specific time implies a date internally set to 01.01.1970 plus the number of
  16. seconds equal to the elapsed hours, minutes, and seconds identified by the time. Normally,
  17. people measure things from a starting point, such as the year 0 A.D. However, many software
  18. systems use the first second of the year 1970 as the starting point, and denote times as a
  19. timestamp offset counting the number of seconds elapsed from this starting point.
  20. </para>
  21. <sect2 id="zend.date.basic.creation">
  22. <title>Current Date</title>
  23. <para>
  24. Without any arguments, constructing an instance returns an object in the default locale
  25. with the current, local date using <acronym>PHP</acronym>'s
  26. <methodname>time()</methodname> function to obtain the <ulink
  27. url="http://en.wikipedia.org/wiki/Unix_Time">UNIX timestamp</ulink>
  28. for the object. Make sure your <acronym>PHP</acronym> environment has the correct
  29. <link linkend="zend.date.setdefaulttimezone">default timezone</link>.
  30. </para>
  31. <example id="zend.date.basic.creation.example-1">
  32. <title>Creating the Current Date</title>
  33. <programlisting language="php"><![CDATA[
  34. $date = new Zend_Date();
  35. // Output of the current timestamp
  36. print $date;
  37. ]]></programlisting>
  38. </example>
  39. </sect2>
  40. <sect2 id="zend.date.basic.functions">
  41. <title>Zend_Date by Example</title>
  42. <para>
  43. Reviewing basic methods of <classname>Zend_Date</classname> is a good place to start for
  44. those unfamiliar with date objects in other languages or frameworks. A small example
  45. will be provided for each method below.
  46. </para>
  47. <sect3 id="zend.date.simple.functions.get">
  48. <title>Output a Date</title>
  49. <para>
  50. The date in a <classname>Zend_Date</classname> object may be obtained as a localized
  51. integer or string using the <methodname>get()</methodname> method. There are many
  52. available options, which will be explained in later sections.
  53. </para>
  54. <example id="zend.date.simple.functions.get.example-1">
  55. <title>get() - Output a Date</title>
  56. <programlisting language="php"><![CDATA[
  57. $date = new Zend_Date();
  58. // Output of the desired date
  59. print $date->get();
  60. ]]></programlisting>
  61. </example>
  62. </sect3>
  63. <sect3 id="zend.date.simple.functions.set">
  64. <title>Setting a Date</title>
  65. <para>
  66. The <methodname>set()</methodname> method alters the date stored in the object, and
  67. returns the final date value as a timestamp (not an object). Again, there are many
  68. options which will be explored in later sections.
  69. </para>
  70. <example id="zend.date.simple.functions.set.example-1">
  71. <title>set() - Set a Date</title>
  72. <programlisting language="php"><![CDATA[
  73. $date = new Zend_Date();
  74. // Setting of a new time
  75. $date->set('13:00:00',Zend_Date::TIMES);
  76. print $date->get(Zend_Date::W3C);
  77. ]]></programlisting>
  78. </example>
  79. </sect3>
  80. <sect3 id="zend.date.simple.functions.add">
  81. <title>Adding and Subtracting Dates</title>
  82. <para>
  83. Adding two dates with <methodname>add()</methodname> usually involves adding a real
  84. date in time with an artificial timestramp representing a date part, such as 12
  85. hours, as shown in the example below. Both <methodname>add()</methodname> and
  86. <methodname>sub()</methodname> use the same set of options as
  87. <methodname>set()</methodname>, which will be explained later.
  88. </para>
  89. <example id="zend.date.simple.functions.add.example-1">
  90. <title>add() - Adding Dates</title>
  91. <programlisting language="php"><![CDATA[
  92. $date = new Zend_Date();
  93. // changes $date by adding 12 hours
  94. $date->add('12:00:00', Zend_Date::TIMES);
  95. echo "Date via get() = ", $date->get(Zend_Date::W3C), "\n";
  96. // use magic __toString() method to call Zend_Date's toString()
  97. echo "Date via toString() = ", $date, "\n";
  98. ]]></programlisting>
  99. </example>
  100. </sect3>
  101. <sect3 id="zend.date.simple.functions.compare">
  102. <title>Comparison of Dates</title>
  103. <para>
  104. All basic <classname>Zend_Date</classname> methods can operate on entire dates
  105. contained in the objects, or can operate on date parts, such as comparing the
  106. minutes value in a date to an absolute value. For example, the current minutes in
  107. the current time may be compared with a specific number of minutes using
  108. <methodname>compare()</methodname>, as in the example below.
  109. </para>
  110. <example id="zend.date.simple.functions.compare.example-1">
  111. <title>compare() - Compare Dates</title>
  112. <programlisting language="php"><![CDATA[
  113. $date = new Zend_Date();
  114. // Comparation of both times
  115. if ($date->compare(10, Zend_Date::MINUTE) == -1) {
  116. print "This hour is less than 10 minutes old";
  117. } else {
  118. print "This hour is at least 10 minutes old";
  119. }
  120. ]]></programlisting>
  121. </example>
  122. <para>
  123. For simple equality comparisons, use <methodname>equals()</methodname>, which
  124. returns a boolean.
  125. </para>
  126. <example id="zend.date.simple.functions.compare.example-2">
  127. <title>equals() - Identify a Date or Date Part</title>
  128. <programlisting language="php"><![CDATA[
  129. $date = new Zend_Date();
  130. // Comparation of the two dates
  131. if ($date->equals(10, Zend_Date::HOUR)) {
  132. print "It's 10 o'clock. Time to get to work.";
  133. } else {
  134. print "It is not 10 o'clock. You can keep sleeping.";
  135. }
  136. ]]></programlisting>
  137. </example>
  138. </sect3>
  139. </sect2>
  140. </sect1>
  141. <!--
  142. vim:se ts=4 sw=4 et:
  143. -->