1 <?xml version="1.0" encoding="UTF-8"?>
3 <sect1 id="zend.date.additional">
4 <title>Working Examples</title>
7 Within this chapter, we will describe several additional functions which are also available
8 through <classname>Zend_Date</classname>. Of course all described functions have additional
9 examples to show the expected working and the simple <acronym>API</acronym> for the proper
13 <sect2 id="zend.date.additional.checking">
14 <title>Checking Dates</title>
17 Probably most dates you will get as input are strings. But the problem with strings is
18 that you can not be sure if the string is a real date. Therefor
19 <classname>Zend_Date</classname> has spent an own static function to check date strings.
20 <classname>Zend_Locale</classname> has an own function
21 <methodname>getDate($date, $locale)</methodname> which parses a date and returns the
22 proper and normalized date parts. A monthname for example will be recognised and
23 returned just a month number. But as <classname>Zend_Locale</classname> does not know
24 anything about dates because it is a normalizing and localizing class we have
25 integrated an own function <methodname>isDate($date)</methodname> which checks this.
29 <methodname>isDate($date, $format, $locale)</methodname> can take up to 3 parameters
30 and needs minimum one parameter. So what we need to verify a date is, of course, the
31 date itself as string. The second parameter can be the format which the date is
32 expected to have. If no format is given the standard date format from your locale is
33 used. For details about how formats should look like see the chapter about <link
34 linkend="zend.date.constants.selfdefinedformats">self defined formats</link>.
38 The third parameter is also optional as the second parameter and can be used to give a
39 locale. We need the locale to normalize monthnames and daynames. So with the third
40 parameter we are able to recognise dates like '<command>01.Jänner.2000</command>' or
41 '<command>01.January.2000</command>' depending on the given locale.
45 <methodname>isDate()</methodname> of course checks if a date does exist.
46 <classname>Zend_Date</classname> itself does not check a date. So it is possible to
47 create a date like '<command>31.February.2000</command>' with
48 <classname>Zend_Date</classname> because <classname>Zend_Date</classname> will
49 automatically correct the date and return the proper date. In our case
50 '<command>03.March.2000</command>'. <methodname>isDate()</methodname> on the
51 other side does this check and will return <constant>FALSE</constant> on
52 '<command>31.February.2000</command>' because it knows that this date is impossible.
55 <example id="zend.date.additional.checking.example-1">
56 <title>Checking Dates</title>
58 <programlisting language="php"><![CDATA[
61 if (Zend_Date::isDate($date)) {
62 print "String $date is a date";
64 print "String $date is NO date";
67 // Checking localized dates
68 $date = '01 February 2000';
69 if (Zend_Date::isDate($date,'dd MMMM yyyy', 'en')) {
70 print "String $date is a date";
72 print "String $date is NO date";
75 // Checking impossible dates
76 $date = '30 February 2000';
77 if (Zend_Date::isDate($date,'dd MMMM yyyy', 'en')) {
78 print "String $date is a date";
80 print "String $date is NO date";
86 <sect2 id="zend.date.additional.sunrise-sunset">
87 <title>Sunrise and Sunset</title>
90 <classname>Zend_Date</classname> has also functions integrated for getting informations
91 from the sun. Often it is necessary to get the time for sunrise or sunset within a
92 particularly day. This is quite easy with <classname>Zend_Date</classname> as just the
93 expected day has to be given and additionally location for which the sunrise or sunset
98 As most people do not know the location of their city we have also spent a helper class
99 which provides the location data for about 250 capital and other big cities around the
100 whole world. Most people could use cities near themself as the difference for locations
101 situated to each other can only be measured within some seconds.
105 For creating a listbox and choosing a special city the function
106 <methodname>Zend_Date_Cities::getCityList()</methodname> can be used. It returns the
107 names of all available predefined cities for the helper class.
110 <example id="zend.date.additional.sunrise-sunset.example-1">
111 <title>Getting all Available Cities</title>
113 <programlisting language="php"><![CDATA[
114 // Output the complete list of available cities
115 print_r (Zend_Date_Cities::getCityList());
120 The location itself can be received with the
121 <methodname>Zend_Date_Cities::city()</methodname> function. It accepts the name of the
122 city as returned by the <methodname>Zend_Date_Cities::getCityList()</methodname>
123 function and optional as second parameter the horizon to set.
127 There are 4 defined horizons which can be used with locations to receive the exact time
128 of sunset and sunrise. The '<varname>$horizon</varname>' parameter is always optional in
129 all functions. If it is not set, the '<property>effective</property>' horizon is used.
132 <table id="zend.date.additional.sunrise-sunset.table">
133 <title>Types of Supported Horizons for Sunset and Sunrise</title>
138 <entry>Horizon</entry>
139 <entry>Description</entry>
146 <entry>effective</entry>
147 <entry>Standard horizon</entry>
150 Expects the world to be a ball. This horizon is always used if non is
157 <entry>Common horizon</entry>
158 <entry>Often used in common medias like TV or radio</entry>
162 <entry>nautic</entry>
163 <entry>Nautic horizon</entry>
164 <entry>Often used in sea navigation</entry>
168 <entry>astronomic</entry>
169 <entry>Astronomic horizon</entry>
170 <entry>Often used for calculation with stars</entry>
177 Of course also a self-defined location can be given and calculated with. Therefor a
178 '<property>latitude</property>' and a '<property>longitude</property>' has to be given
179 and optional the '<property>horizon</property>'.
182 <example id="zend.date.additional.sunrise-sunset.example-2">
183 <title>Getting the Location for a City</title>
185 <programlisting language="php"><![CDATA[
186 // Get the location for a defined city
187 // uses the effective horizon as no horizon is defined
188 print_r (Zend_Date_Cities::city('Vienna'));
190 // use the nautic horizon
191 print_r (Zend_Date_Cities::city('Vienna', 'nautic'));
193 // self definition of a location
194 $mylocation = array('latitude' => 41.5, 'longitude' => 13.2446);
199 As now all needed data can be set the next is to create a
200 <classname>Zend_Date</classname> object with the day where sunset or sunrise should be
201 calculated. For the calculation there are 3 functions available. It is possible to
202 calculate sunset with '<methodname>getSunset()</methodname>', sunrise with
203 '<methodname>getSunrise()</methodname>' and all available informations related to the
204 sun with '<methodname>getSunInfo()</methodname>'. After the calculation the
205 <classname>Zend_Date</classname> object will be returned with the calculated time.
208 <example id="zend.date.additional.sunrise-sunset.example-3">
209 <title>Calculating Sun Information</title>
211 <programlisting language="php"><![CDATA[
212 // Get the location for a defined city
213 $city = Zend_Date_Cities::city('Vienna');
215 // create a date object for the day for which the sun has to be calculated
216 $date = new Zend_Date('10.03.2007', Zend_Date::ISO_8601, 'de');
219 $sunset = $date->getSunset($city);
220 print $sunset->get(Zend_Date::ISO_8601);
222 // calculate all sun informations
223 $info = $date->getSunInfo($city);
224 foreach ($info as $sun) {
225 print "\n" . $sun->get(Zend_Date::ISO_8601);
231 <sect2 id="zend.date.additional.timezones">
232 <title>Time Zones</title>
235 Time zones are as important as dates themselves. There are several time zones depending
236 on where in the world a user lives. So working with dates also means to set the proper
237 timezone. This may sound complicated but it's easier as expected. As already mentioned
238 in the first chapter of <classname>Zend_Date</classname> the default timezone has to be
239 set. Either by <filename>php.ini</filename> or by definition within the bootstrap file.
243 A <classname>Zend_Date</classname> object of course also stores the actual timezone.
244 Even if the timezone is changed after the creation of the object it remembers the
245 original timezone and works with it. It is also not necessary to change the timezone
246 within the code with <acronym>PHP</acronym> functions. <classname>Zend_Date</classname>
247 has two built-in functions which makes it possible to handle this.
251 <methodname>getTimezone()</methodname> returns the actual set timezone of within the
252 <classname>Zend_Date</classname> object. Remember that <classname>Zend_Date</classname>
253 is not coupled with <acronym>PHP</acronym> internals. So the returned timezone is not
254 the timezone of the <acronym>PHP</acronym> script but the timezone of the object.
255 <methodname>setTimezone($zone)</methodname> is the second function and makes it possible
256 to set new timezone for <classname>Zend_Date</classname>. A given timezone is always
257 checked. If it does not exist an exception will be thrown. Additionally the actual
258 scripts or systems timezone can be set to the date object by calling
259 <methodname>setTimezone()</methodname> without the zone parameter. This is also done
260 automatically when the date object is created.
263 <example id="zend.date.additional.timezones.example-1">
264 <title>Working with Time Zones</title>
266 <programlisting language="php"><![CDATA[
267 // Set a default timezone... this has to be done within the bootstrap
269 // We do this here just for having a complete example.
270 date_default_timezone_set('Europe/Vienna');
272 // create a date object
273 $date = new Zend_Date('10.03.2007', Zend_Date::DATES, 'de');
275 // view our date object
276 print $date->getIso();
278 // what timezone do we have ?
279 print $date->getTimezone();
281 // set another timezone
282 $date->setTimezone('America/Chicago');
284 // what timezone do we now have ?
285 print $date->getTimezone();
287 // see the changed date object
288 print $date->getIso();
293 <classname>Zend_Date</classname> always takes the actual timezone for object creation as
294 shown in the first lines of the example. Changing the timezone within the created object
295 also has an effect to the date itself. Dates are always related to a timezone. Changing
296 the timezone for a <classname>Zend_Date</classname> object does not change the time of
297 <classname>Zend_Date</classname>. Remember that internally dates are always stored as
298 timestamps and in <acronym>GMT</acronym>. So the timezone means how much hours should be
299 substracted or added to get the actual global time for the own timezone and region.
303 Having the timezone coupled within <classname>Zend_Date</classname> has another positive
304 effect. It is possible to have several dates with different timezones.
307 <example id="zend.date.additional.timezones.example-2">
308 <title>Multiple Time Zones</title>
310 <programlisting language="php"><![CDATA[
311 // Set a default timezone... this has to be done within the bootstrap
313 // We do this here just for having a complete example.
314 date_default_timezone_set('Europe/Vienna');
316 // create a date object
317 $date = new Zend_Date('10.03.2007 00:00:00', Zend_Date::ISO_8601, 'de');
319 // view our date object
320 print $date->getIso();
322 // the date stays unchanged even after changeing the timezone
323 date_default_timezone_set('America/Chicago');
324 print $date->getIso();
326 $otherdate = clone $date;
327 $otherdate->setTimezone('Brazil/Acre');
329 // view our date object
330 print $otherdate->getIso();
332 // set the object to the actual systems timezone
333 $lastdate = clone $date;
334 $lastdate->setTimezone();
336 // view our date object
337 print $lastdate->getIso();