2
0

Zend_Date-Creation.xml 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="zend.date.creation">
  4. <title>Creation of Dates</title>
  5. <para>
  6. <classname>Zend_Date</classname> provides several different ways to create a new instance of
  7. itself. As there are different needs the most convenient ways will be shown in this chapter.
  8. </para>
  9. <sect2 id="zend.date.creation.actual">
  10. <title>Create the Actual Date</title>
  11. <para>
  12. The simplest way of creating a date object is to create the actual date. You can either
  13. create a new instance with <emphasis>new Zend_Date()</emphasis> or use the convenient
  14. static method <emphasis>Zend_Date::now()</emphasis> which both will return the actual
  15. date as new instance of <classname>Zend_Date</classname>. The actual date always include
  16. the actual date and time for the actual set timezone.
  17. </para>
  18. <example id="zend.date.creation.actual.example-1">
  19. <title>Date Creation by Instance</title>
  20. <para>
  21. Date creation by creating a new instance means that you do not need to give an
  22. parameter. Of course there are several parameters which will be described later but
  23. normally this is the simplest and most used way to get the actual date as
  24. <classname>Zend_Date</classname> instance.
  25. </para>
  26. <programlisting language="php"><![CDATA[
  27. $date = new Zend_Date();
  28. ]]></programlisting>
  29. </example>
  30. <example id="zend.date.creation.actual.example-2">
  31. <title>Static Date Creation</title>
  32. <para>
  33. Sometimes it is easier to use a static method for date creation. Therefor you can
  34. use the <emphasis><code>now()</code></emphasis> method. It returns a new instance of
  35. <classname>Zend_Date</classname> the same way as if you would use
  36. <code>new Zend_Date()</code>. But it will always return the actual date and can not
  37. be changed by giving optional parameters.
  38. </para>
  39. <programlisting language="php"><![CDATA[
  40. $date = Zend_Date::now();
  41. ]]></programlisting>
  42. </example>
  43. </sect2>
  44. <sect2 id="zend.date.creation.database">
  45. <title>Create a Date from Database</title>
  46. <para>
  47. Databases are often used to store date values. But the problem is, that every database
  48. outputs its date values in a different way. <code>MsSQL</code> databases use a quite
  49. different standard date output than <code>MySQL</code> databases. But for simplification
  50. <classname>Zend_Date</classname> makes it very easy to create a date from database date
  51. values.
  52. </para>
  53. <para>
  54. Of course each database can be said to convert the output of a defined column to a
  55. special value. For example you could convert a <code>datetime</code> value to output a
  56. minute value. But this is time expensive and often you are in need of handling dates in
  57. an other way than expected when creating the database query.
  58. </para>
  59. <para>
  60. So we have one quick and one convenient way of creating dates from database values.
  61. </para>
  62. <example id="zend.date.creation.database.example-1">
  63. <title>Quick Creation of Dates from Database Date Values</title>
  64. <para>
  65. All databases are known to handle queries as fast as possible. They are built to act
  66. and respond quick. The quickest way for handling dates is to get unix timestamps
  67. from the database. All databases store date values internal as timestamp (not unix
  68. timestamp). This means that the time for creating a timestamp through a query is
  69. much smaller than converting it to a specified format.
  70. </para>
  71. <programlisting language="php"><![CDATA[
  72. // SELECT UNIX_TIMESTAMP(my_datetime_column) FROM my_table
  73. $date = new Zend_Date($unixtimestamp, Zend_Date::TIMESTAMP);
  74. ]]></programlisting>
  75. </example>
  76. <example id="zend.date.creation.database.example-2">
  77. <title>Convenient Creation of Dates from Database Date Values</title>
  78. <para>
  79. The standard output of all databases is quite different even if it looks the same on
  80. the first eyecatch. But all are part of the <code>ISO</code> Standard and explained
  81. through it. So the easiest way of date creation is the usage of
  82. <classname>Zend_Date::ISO_8601</classname>. Databases which are known to be
  83. recognised by <classname>Zend_Date::ISO_8601</classname> are <code>MySQL</code>,
  84. <code>MsSQL</code> for example. But all databases are also able to return a
  85. <code>ISO 8601</code> representation of a date column. <code>ISO 8601</code> has the
  86. big advantage that it is human readable. The disadvantage is that <code>ISO
  87. 8601</code> needs more time for computation than a simple unix timestamp. But it
  88. should also be mentioned that unix timestamps are only supported for dates after 1
  89. January 1970.
  90. </para>
  91. <programlisting language="php"><![CDATA[
  92. // SELECT datecolumn FROM my_table
  93. $date = new Zend_Date($datecolumn, Zend_Date::ISO_8601);
  94. ]]></programlisting>
  95. </example>
  96. </sect2>
  97. <sect2 id="zend.date.creation.array">
  98. <title>Create Dates from an Array</title>
  99. <para>
  100. Dates can also be created by the usage of an array. This is a simple and easy way. The
  101. used array keys are:
  102. </para>
  103. <para>
  104. <itemizedlist mark='opencircle'>
  105. <listitem>
  106. <para>
  107. <emphasis>day</emphasis>: day of the date as number
  108. </para>
  109. </listitem>
  110. <listitem>
  111. <para>
  112. <emphasis>month</emphasis>: month of the date as number
  113. </para>
  114. </listitem>
  115. <listitem>
  116. <para>
  117. <emphasis>year</emphasis>: full year of the date
  118. </para>
  119. </listitem>
  120. <listitem>
  121. <para>
  122. <emphasis>hour</emphasis>: hour of the date
  123. </para>
  124. </listitem>
  125. <listitem>
  126. <para>
  127. <emphasis>minute</emphasis>: minute of the date
  128. </para>
  129. </listitem>
  130. <listitem>
  131. <para>
  132. <emphasis>second</emphasis>: second of the date
  133. </para>
  134. </listitem>
  135. </itemizedlist>
  136. </para>
  137. <example id="zend.date.creation.array.example">
  138. <title>Date Creation by Array</title>
  139. <para>
  140. Normally you will give a complete date array for creation of a new date instance.
  141. But when you do not give all values, the not given array values are zeroed. This
  142. means that if f.e. no hour is given the hour <emphasis>0</emphasis> is used.
  143. </para>
  144. <programlisting language="php"><![CDATA[
  145. $datearray = array('year' => 2006,
  146. 'month' => 4,
  147. 'day' => 18,
  148. 'hour' => 12,
  149. 'minute' => 3,
  150. 'second' => 10);
  151. $date = new Zend_Date($datearray);
  152. ]]></programlisting>
  153. <programlisting language="php"><![CDATA[
  154. $datearray = array('year' => 2006, 'month' => 4, 'day' => 18);
  155. $date = new Zend_Date($datearray);
  156. ]]></programlisting>
  157. </example>
  158. </sect2>
  159. </sect1>
  160. <!--
  161. vim:se ts=4 sw=4 et:
  162. -->