Zend_Date-Basic.xml 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. .
  31. </para>
  32. <example id="zend.date.basic.creation.example-1">
  33. <title>Creating the Current Date</title>
  34. <programlisting language="php"><![CDATA[
  35. $date = new Zend_Date();
  36. // Output of the current timestamp
  37. print $date;
  38. ]]></programlisting>
  39. </example>
  40. </sect2>
  41. <sect2 id="zend.date.basic.functions">
  42. <title>Zend_Date by Example</title>
  43. <para>
  44. Reviewing basic methods of <classname>Zend_Date</classname> is a good place to start for
  45. those unfamiliar with date objects in other languages or frameworks. A small example
  46. will be provided for each method below.
  47. </para>
  48. <sect3 id="zend.date.simple.functions.get">
  49. <title>Output a Date</title>
  50. <para>
  51. The date in a <classname>Zend_Date</classname> object may be obtained as a localized
  52. integer or string using the <methodname>get()</methodname> method. There are many
  53. available options, which will be explained in later sections.
  54. </para>
  55. <example id="zend.date.simple.functions.get.example-1">
  56. <title>get() - Output a Date</title>
  57. <programlisting language="php"><![CDATA[
  58. $date = new Zend_Date();
  59. // Output of the desired date
  60. print $date->get();
  61. ]]></programlisting>
  62. </example>
  63. </sect3>
  64. <sect3 id="zend.date.simple.functions.set">
  65. <title>Setting a Date</title>
  66. <para>
  67. The <methodname>set()</methodname> method alters the date stored in the object, and
  68. returns the final date value as a timestamp (not an object). Again, there are many
  69. options which will be explored in later sections.
  70. </para>
  71. <example id="zend.date.simple.functions.set.example-1">
  72. <title>set() - Set a Date</title>
  73. <programlisting language="php"><![CDATA[
  74. $date = new Zend_Date();
  75. // Setting of a new time
  76. $date->set('13:00:00',Zend_Date::TIMES);
  77. print $date->get(Zend_Date::W3C);
  78. ]]></programlisting>
  79. </example>
  80. </sect3>
  81. <sect3 id="zend.date.simple.functions.add">
  82. <title>Adding and Subtracting Dates</title>
  83. <para>
  84. Adding two dates with <methodname>add()</methodname> usually involves adding a real
  85. date in time with an artificial timestramp representing a date part, such as 12
  86. hours, as shown in the example below. Both <methodname>add()</methodname> and
  87. <methodname>sub()</methodname> use the same set of options as
  88. <methodname>set()</methodname>, which will be explained later.
  89. </para>
  90. <example id="zend.date.simple.functions.add.example-1">
  91. <title>add() - Adding Dates</title>
  92. <programlisting language="php"><![CDATA[
  93. $date = new Zend_Date();
  94. // changes $date by adding 12 hours
  95. $date->add('12:00:00', Zend_Date::TIMES);
  96. echo "Date via get() = ", $date->get(Zend_Date::W3C), "\n";
  97. // use magic __toString() method to call Zend_Date's toString()
  98. echo "Date via toString() = ", $date, "\n";
  99. ]]></programlisting>
  100. </example>
  101. </sect3>
  102. <sect3 id="zend.date.simple.functions.compare">
  103. <title>Comparison of Dates</title>
  104. <para>
  105. All basic <classname>Zend_Date</classname> methods can operate on entire dates
  106. contained in the objects, or can operate on date parts, such as comparing the
  107. minutes value in a date to an absolute value. For example, the current minutes in
  108. the current time may be compared with a specific number of minutes using
  109. <methodname>compare()</methodname>, as in the example below.
  110. </para>
  111. <example id="zend.date.simple.functions.compare.example-1">
  112. <title>compare() - Compare Dates</title>
  113. <programlisting language="php"><![CDATA[
  114. $date = new Zend_Date();
  115. // Comparation of both times
  116. if ($date->compare(10, Zend_Date::MINUTE) == -1) {
  117. print "This hour is less than 10 minutes old";
  118. } else {
  119. print "This hour is at least 10 minutes old";
  120. }
  121. ]]></programlisting>
  122. </example>
  123. <para>
  124. For simple equality comparisons, use <methodname>equals()</methodname>, which
  125. returns a boolean.
  126. </para>
  127. <example id="zend.date.simple.functions.compare.example-2">
  128. <title>equals() - Identify a Date or Date Part</title>
  129. <programlisting language="php"><![CDATA[
  130. $date = new Zend_Date();
  131. // Comparation of the two dates
  132. if ($date->equals(10, Zend_Date::HOUR)) {
  133. print "It's 10 o'clock. Time to get to work.";
  134. } else {
  135. print "It is not 10 o'clock. You can keep sleeping.";
  136. }
  137. ]]></programlisting>
  138. </example>
  139. </sect3>
  140. </sect2>
  141. </sect1>
  142. <!--
  143. vim:se ts=4 sw=4 et:
  144. -->