Zend_Date-Creation.xml 7.8 KB

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