2.19 Finding the Date and Time for Different Locales


You want to find the date and time in Finland , but your servers are in Texas.

Technique

Write your own universaltime() function utilizing mktime () , gmdate() , and getdate() :

 <?php function universaltime ($offset) {     $day     = gmdate("d");     $month   = gmdate("m");     $hour    = gmdate("g");     $year    = gmdate("Y");     $minutes = gmdate("i");     $seconds = gmdate("s");     $hour    = gmdate("H") + $offset;     $tm_ar = getdate (mktime ($hour,                               $minutes,                               $seconds,                               $month,                               $day,                               $year));     return ($tm_ar); } $currdate = universaltime ($offset); ?> 

Comments

First, we find the GMT, or Greenwich mean time, the standard to which all time zones are compared. This is accomplished using the gmdate() function, which takes the same arguments as the date() function that was discussed partially in the last recipe and will be discussed fully in the next recipe.

Then we increment the hour by the hour offset from Greenwich mean time (the offset is passed to the function). Some of you might be wondering what happens if the change in hour affects the change in day, month, or year. No problem, the mktime() function automatically corrects for these differences.

We then use the getdate() function, which takes an optional UNIX timestamp created by the mktime() function. It returns an associative array with the following contents: seconds, minutes, hours, mday (day of the month), wday (day of the week, numeric), year, yday (day of the year), weekday (day of week, textual), and month (month, textual).

So, when we go back to the original problem to find the date and time in Finland, it is a cinch. All that we need to know is the GMT offset (which is 2 ) and then call our universaltime() function like so

 <?php $finland_date = universaltime(2); ?> 


PHP Developer's Cookbook
PHP Developers Cookbook (2nd Edition)
ISBN: 0672323257
EAN: 2147483647
Year: 2000
Pages: 351

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net