Zend_Date-Additional.xml 14 KB

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