Zend_Date-Creation.xml 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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 <command>new Zend_Date()</command> or use the convenient
  14. static method <methodname>Zend_Date::now()</methodname> which both will return the
  15. actual date as new instance of <classname>Zend_Date</classname>. The actual date always
  16. include 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><methodname>now()</methodname></emphasis> method. It returns a
  35. new instance of <classname>Zend_Date</classname> the same way as if you would use
  36. <command>new Zend_Date()</command>. But it will always return the actual date and
  37. can not 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. <emphasis>MsSQL</emphasis> databases use a
  49. quite different standard date output than <emphasis>MySQL</emphasis> databases. But for
  50. simplification <classname>Zend_Date</classname> makes it very easy to create a date
  51. from database date 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 <emphasis>datetime</emphasis> value to
  56. output a minute value. But this is time expensive and often you are in need of handling
  57. dates in 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 <acronym>ISO</acronym> Standard and
  81. explained through it. So the easiest way of date creation is the usage of
  82. <constant>Zend_Date::ISO_8601</constant>. Databases which are known to be
  83. recognised by <constant>Zend_Date::ISO_8601</constant> are
  84. <emphasis>MySQL</emphasis>, <emphasis>MsSQL</emphasis> for example. But all
  85. databases are also able to return a <acronym>ISO-8601</acronym> representation of a
  86. date column. <acronym>ISO-8601</acronym> has the big advantage that it is human
  87. readable. The disadvantage is that <acronym>ISO-8601</acronym> needs more time for
  88. computation than a simple unix timestamp. But it should also be mentioned that unix
  89. timestamps are only supported for dates after 1 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. <itemizedlist mark='opencircle'>
  104. <listitem><para><emphasis>day</emphasis>: day of the date as number</para></listitem>
  105. <listitem>
  106. <para><emphasis>month</emphasis>: month of the date as number</para>
  107. </listitem>
  108. <listitem><para><emphasis>year</emphasis>: full year of the date</para></listitem>
  109. <listitem><para><emphasis>hour</emphasis>: hour of the date</para></listitem>
  110. <listitem><para><emphasis>minute</emphasis>: minute of the date</para></listitem>
  111. <listitem><para><emphasis>second</emphasis>: second of the date</para></listitem>
  112. </itemizedlist>
  113. <example id="zend.date.creation.array.example">
  114. <title>Date Creation by Array</title>
  115. <para>
  116. Normally you will give a complete date array for creation of a new date instance.
  117. But when you do not give all values, the not given array values are zeroed. This
  118. means that if f.e. no hour is given the hour <emphasis>0</emphasis> is used.
  119. </para>
  120. <programlisting language="php"><![CDATA[
  121. $datearray = array('year' => 2006,
  122. 'month' => 4,
  123. 'day' => 18,
  124. 'hour' => 12,
  125. 'minute' => 3,
  126. 'second' => 10);
  127. $date = new Zend_Date($datearray);
  128. ]]></programlisting>
  129. <programlisting language="php"><![CDATA[
  130. $datearray = array('year' => 2006, 'month' => 4, 'day' => 18);
  131. $date = new Zend_Date($datearray);
  132. ]]></programlisting>
  133. </example>
  134. </sect2>
  135. </sect1>
  136. <!--
  137. vim:se ts=4 sw=4 et:
  138. -->