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