Zend_Date-Basic.xml 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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 PHP's <code>time()</code> function to obtain the
  26. <ulink url="http://en.wikipedia.org/wiki/Unix_Time">UNIX timestamp</ulink>
  27. for the object. Make sure your PHP environment has the correct
  28. <link linkend="zend.date.setdefaulttimezone">default timezone</link>
  29. .
  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 <code>get()</code> method. There are many available
  52. 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 <code>set()</code> method alters the date stored in the object, and returns the
  67. final date value as a timestamp (not an object). Again, there are many options which
  68. 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 <code>add()</code> usually involves adding a real date in time
  84. with an artificial timestramp representing a date part, such as 12 hours, as shown
  85. in the example below. Both <code>add()</code> and <code>sub()</code> use the same
  86. set of options as <code>set()</code>, which will be explained later.
  87. </para>
  88. <example id="zend.date.simple.functions.add.example-1">
  89. <title>add() - Adding Dates</title>
  90. <programlisting language="php"><![CDATA[
  91. $date = new Zend_Date();
  92. // changes $date by adding 12 hours
  93. $date->add('12:00:00', Zend_Date::TIMES);
  94. echo "Date via get() = ", $date->get(Zend_Date::W3C), "\n";
  95. // use magic __toString() method to call Zend_Date's toString()
  96. echo "Date via toString() = ", $date, "\n";
  97. ]]></programlisting>
  98. </example>
  99. </sect3>
  100. <sect3 id="zend.date.simple.functions.compare">
  101. <title>Comparison of Dates</title>
  102. <para>
  103. All basic <classname>Zend_Date</classname> methods can operate on entire dates
  104. contained in the objects, or can operate on date parts, such as comparing the
  105. minutes value in a date to an absolute value. For example, the current minutes in
  106. the current time may be compared with a specific number of minutes using
  107. <code>compare()</code>, as in the example below.
  108. </para>
  109. <example id="zend.date.simple.functions.compare.example-1">
  110. <title>compare() - Compare Dates</title>
  111. <programlisting language="php"><![CDATA[
  112. $date = new Zend_Date();
  113. // Comparation of both times
  114. if ($date->compare(10, Zend_Date::MINUTE) == -1) {
  115. print "This hour is less than 10 minutes old";
  116. } else {
  117. print "This hour is at least 10 minutes old";
  118. }
  119. ]]></programlisting>
  120. </example>
  121. <para>
  122. For simple equality comparisons, use <code>equals()</code>, which returns a boolean.
  123. </para>
  124. <example id="zend.date.simple.functions.compare.example-2">
  125. <title>equals() - Identify a Date or Date Part</title>
  126. <programlisting language="php"><![CDATA[
  127. $date = new Zend_Date();
  128. // Comparation of the two dates
  129. if ($date->equals(10, Zend_Date::HOUR)) {
  130. print "It's 10 o'clock. Time to get to work.";
  131. } else {
  132. print "It is not 10 o'clock. You can keep sleeping.";
  133. }
  134. ]]></programlisting>
  135. </example>
  136. </sect3>
  137. </sect2>
  138. </sect1>
  139. <!--
  140. vim:se ts=4 sw=4 et:
  141. -->