Zend_Date-Additional.xml 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="zend.date.additional">
  4. <title>Working Examples</title>
  5. <para>
  6. Within this chapter, we will describe several additional functions which are also available
  7. through <classname>Zend_Date</classname>. Of course all described functions have additional
  8. examples to show the expected working and the simple <acronym>API</acronym> for the proper
  9. using of them.
  10. </para>
  11. <sect2 id="zend.date.additional.checking">
  12. <title>Checking Dates</title>
  13. <para>
  14. Probably most dates you will get as input are strings. But the problem with strings is
  15. that you can not be sure if the string is a real date. Therefor
  16. <classname>Zend_Date</classname> has spent an own static function to check date strings.
  17. <classname>Zend_Locale</classname> has an own function
  18. <methodname>getDate($date, $locale)</methodname> which parses a date and returns the
  19. proper and normalized date parts. A monthname for example will be recognised and
  20. returned just a month number. But as <classname>Zend_Locale</classname> does not know
  21. anything about dates because it is a normalizing and localizing class we have
  22. integrated an own function <methodname>isDate($date)</methodname> which checks this.
  23. </para>
  24. <para>
  25. <methodname>isDate($date, $format, $locale)</methodname> can take up to 3 parameters
  26. and needs minimum one parameter. So what we need to verify a date is, of course, the
  27. date itself as string. The second parameter can be the format which the date is
  28. expected to have. If no format is given the standard date format from your locale is
  29. used. For details about how formats should look like see the chapter about <link
  30. linkend="zend.date.constants.selfdefinedformats">self defined formats</link>.
  31. </para>
  32. <para>
  33. The third parameter is also optional as the second parameter and can be used to give a
  34. locale. We need the locale to normalize monthnames and daynames. So with the third
  35. parameter we are able to recognise dates like '<command>01.Jänner.2000</command>' or
  36. '<command>01.January.2000</command>' depending on the given locale.
  37. </para>
  38. <para>
  39. <methodname>isDate()</methodname> of course checks if a date does exist.
  40. <classname>Zend_Date</classname> itself does not check a date. So it is possible to
  41. create a date like '<command>31.February.2000</command>' with
  42. <classname>Zend_Date</classname> because <classname>Zend_Date</classname> will
  43. automatically correct the date and return the proper date. In our case
  44. '<command>03.March.2000</command>'. <methodname>isDate()</methodname> on the
  45. other side does this check and will return <constant>FALSE</constant> on
  46. '<command>31.February.2000</command>' because it knows that this date is impossible.
  47. </para>
  48. <example id="zend.date.additional.checking.example-1">
  49. <title>Checking Dates</title>
  50. <programlisting language="php"><![CDATA[
  51. // Checking dates
  52. $date = '01.03.2000';
  53. if (Zend_Date::isDate($date)) {
  54. print "String $date is a date";
  55. } else {
  56. print "String $date is NO date";
  57. }
  58. // Checking localized dates
  59. $date = '01 February 2000';
  60. if (Zend_Date::isDate($date,'dd MMMM yyyy', 'en')) {
  61. print "String $date is a date";
  62. } else {
  63. print "String $date is NO date";
  64. }
  65. // Checking impossible dates
  66. $date = '30 February 2000';
  67. if (Zend_Date::isDate($date,'dd MMMM yyyy', 'en')) {
  68. print "String $date is a date";
  69. } else {
  70. print "String $date is NO date";
  71. }
  72. ]]></programlisting>
  73. </example>
  74. </sect2>
  75. <sect2 id="zend.date.additional.sunrise-sunset">
  76. <title>Sunrise and Sunset</title>
  77. <para>
  78. <classname>Zend_Date</classname> has also functions integrated for getting informations
  79. from the sun. Often it is necessary to get the time for sunrise or sunset within a
  80. particularly day. This is quite easy with <classname>Zend_Date</classname> as just the
  81. expected day has to be given and additionally location for which the sunrise or sunset
  82. has to be calculated.
  83. </para>
  84. <para>
  85. As most people do not know the location of their city we have also spent a helper class
  86. which provides the location data for about 250 capital and other big cities around the
  87. whole world. Most people could use cities near themself as the difference for locations
  88. situated to each other can only be measured within some seconds.
  89. </para>
  90. <para>
  91. For creating a listbox and choosing a special city the function
  92. <methodname>Zend_Date_Cities::getCityList()</methodname> can be used. It returns the
  93. names of all available predefined cities for the helper class.
  94. </para>
  95. <example id="zend.date.additional.sunrise-sunset.example-1">
  96. <title>Getting all Available Cities</title>
  97. <programlisting language="php"><![CDATA[
  98. // Output the complete list of available cities
  99. print_r (Zend_Date_Cities::getCityList());
  100. ]]></programlisting>
  101. </example>
  102. <para>
  103. The location itself can be received with the
  104. <methodname>Zend_Date_Cities::city()</methodname> function. It accepts the name of the
  105. city as returned by the <methodname>Zend_Date_Cities::getCityList()</methodname>
  106. function and optional as second parameter the horizon to set.
  107. </para>
  108. <para>
  109. There are 4 defined horizons which can be used with locations to receive the exact time
  110. of sunset and sunrise. The '<varname>$horizon</varname>' parameter is always optional in
  111. all functions. If it is not set, the '<property>effective</property>' horizon is used.
  112. </para>
  113. <table id="zend.date.additional.sunrise-sunset.table">
  114. <title>Types of Supported Horizons for Sunset and Sunrise</title>
  115. <tgroup cols="3">
  116. <thead>
  117. <row>
  118. <entry>Horizon</entry>
  119. <entry>Description</entry>
  120. <entry>Usage</entry>
  121. </row>
  122. </thead>
  123. <tbody>
  124. <row>
  125. <entry>effective</entry>
  126. <entry>Standard horizon</entry>
  127. <entry>
  128. Expects the world to be a ball. This horizon is always used if non is
  129. defined.
  130. </entry>
  131. </row>
  132. <row>
  133. <entry>civil</entry>
  134. <entry>Common horizon</entry>
  135. <entry>Often used in common medias like TV or radio</entry>
  136. </row>
  137. <row>
  138. <entry>nautic</entry>
  139. <entry>Nautic horizon</entry>
  140. <entry>Often used in sea navigation</entry>
  141. </row>
  142. <row>
  143. <entry>astronomic</entry>
  144. <entry>Astronomic horizon</entry>
  145. <entry>Often used for calculation with stars</entry>
  146. </row>
  147. </tbody>
  148. </tgroup>
  149. </table>
  150. <para>
  151. Of course also a self-defined location can be given and calculated with. Therefor a
  152. '<property>latitude</property>' and a '<property>longitude</property>' has to be given
  153. and optional the '<property>horizon</property>'.
  154. </para>
  155. <example id="zend.date.additional.sunrise-sunset.example-2">
  156. <title>Getting the Location for a City</title>
  157. <programlisting language="php"><![CDATA[
  158. // Get the location for a defined city
  159. // uses the effective horizon as no horizon is defined
  160. print_r (Zend_Date_Cities::city('Vienna'));
  161. // use the nautic horizon
  162. print_r (Zend_Date_Cities::city('Vienna', 'nautic'));
  163. // self definition of a location
  164. $mylocation = array('latitude' => 41.5, 'longitude' => 13.2446);
  165. ]]></programlisting>
  166. </example>
  167. <para>
  168. As now all needed data can be set the next is to create a
  169. <classname>Zend_Date</classname> object with the day where sunset or sunrise should be
  170. calculated. For the calculation there are 3 functions available. It is possible to
  171. calculate sunset with '<methodname>getSunset()</methodname>', sunrise with
  172. '<methodname>getSunrise()</methodname>' and all available informations related to the
  173. sun with '<methodname>getSunInfo()</methodname>'. After the calculation the
  174. <classname>Zend_Date</classname> object will be returned with the calculated time.
  175. </para>
  176. <example id="zend.date.additional.sunrise-sunset.example-3">
  177. <title>Calculating Sun Information</title>
  178. <programlisting language="php"><![CDATA[
  179. // Get the location for a defined city
  180. $city = Zend_Date_Cities::city('Vienna');
  181. // create a date object for the day for which the sun has to be calculated
  182. $date = new Zend_Date('10.03.2007', Zend_Date::ISO_8601, 'de');
  183. // calculate sunset
  184. $sunset = $date->getSunset($city);
  185. print $sunset->get(Zend_Date::ISO_8601);
  186. // calculate all sun informations
  187. $info = $date->getSunInfo($city);
  188. foreach ($info as $sun) {
  189. print "\n" . $sun->get(Zend_Date::ISO_8601);
  190. }
  191. ]]></programlisting>
  192. </example>
  193. </sect2>
  194. <sect2 id="zend.date.additional.timezones">
  195. <title>Time Zones</title>
  196. <para>
  197. Time zones are as important as dates themselves. There are several time zones depending
  198. on where in the world a user lives. So working with dates also means to set the proper
  199. timezone. This may sound complicated but it's easier as expected. As already mentioned
  200. in the first chapter of <classname>Zend_Date</classname> the default timezone has to be
  201. set. Either by <filename>php.ini</filename> or by definition within the bootstrap file.
  202. </para>
  203. <para>
  204. A <classname>Zend_Date</classname> object of course also stores the actual timezone.
  205. Even if the timezone is changed after the creation of the object it remembers the
  206. original timezone and works with it. It is also not necessary to change the timezone
  207. within the code with <acronym>PHP</acronym> functions. <classname>Zend_Date</classname>
  208. has two built-in functions which makes it possible to handle this.
  209. </para>
  210. <para>
  211. <methodname>getTimezone()</methodname> returns the actual set timezone of within the
  212. <classname>Zend_Date</classname> object. Remember that <classname>Zend_Date</classname>
  213. is not coupled with <acronym>PHP</acronym> internals. So the returned timezone is not
  214. the timezone of the <acronym>PHP</acronym> script but the timezone of the object.
  215. <methodname>setTimezone($zone)</methodname> is the second function and makes it possible
  216. to set new timezone for <classname>Zend_Date</classname>. A given timezone is always
  217. checked. If it does not exist an exception will be thrown. Additionally the actual
  218. scripts or systems timezone can be set to the date object by calling
  219. <methodname>setTimezone()</methodname> without the zone parameter. This is also done
  220. automatically when the date object is created.
  221. </para>
  222. <example id="zend.date.additional.timezones.example-1">
  223. <title>Working with Time Zones</title>
  224. <programlisting language="php"><![CDATA[
  225. // Set a default timezone... this has to be done within the bootstrap
  226. // file or php.ini.
  227. // We do this here just for having a complete example.
  228. date_default_timezone_set('Europe/Vienna');
  229. // create a date object
  230. $date = new Zend_Date('10.03.2007', Zend_Date::DATES, 'de');
  231. // view our date object
  232. print $date->getIso();
  233. // what timezone do we have ?
  234. print $date->getTimezone();
  235. // set another timezone
  236. $date->setTimezone('America/Chicago');
  237. // what timezone do we now have ?
  238. print $date->getTimezone();
  239. // see the changed date object
  240. print $date->getIso();
  241. ]]></programlisting>
  242. </example>
  243. <para>
  244. <classname>Zend_Date</classname> always takes the actual timezone for object creation as
  245. shown in the first lines of the example. Changing the timezone within the created object
  246. also has an effect to the date itself. Dates are always related to a timezone. Changing
  247. the timezone for a <classname>Zend_Date</classname> object does not change the time of
  248. <classname>Zend_Date</classname>. Remember that internally dates are always stored as
  249. timestamps and in <acronym>GMT</acronym>. So the timezone means how much hours should be
  250. substracted or added to get the actual global time for the own timezone and region.
  251. </para>
  252. <para>
  253. Having the timezone coupled within <classname>Zend_Date</classname> has another positive
  254. effect. It is possible to have several dates with different timezones.
  255. </para>
  256. <example id="zend.date.additional.timezones.example-2">
  257. <title>Multiple Time Zones</title>
  258. <programlisting language="php"><![CDATA[
  259. // Set a default timezone... this has to be done within the bootstrap
  260. // file or php.ini.
  261. // We do this here just for having a complete example.
  262. date_default_timezone_set('Europe/Vienna');
  263. // create a date object
  264. $date = new Zend_Date('10.03.2007 00:00:00', Zend_Date::ISO_8601, 'de');
  265. // view our date object
  266. print $date->getIso();
  267. // the date stays unchanged even after changeing the timezone
  268. date_default_timezone_set('America/Chicago');
  269. print $date->getIso();
  270. $otherdate = clone $date;
  271. $otherdate->setTimezone('Brazil/Acre');
  272. // view our date object
  273. print $otherdate->getIso();
  274. // set the object to the actual systems timezone
  275. $lastdate = clone $date;
  276. $lastdate->setTimezone();
  277. // view our date object
  278. print $lastdate->getIso();
  279. ]]></programlisting>
  280. </example>
  281. </sect2>
  282. </sect1>
  283. <!--
  284. vim:se ts=4 sw=4 et:
  285. -->