. Updated Daily. Editions SDA India   SDA Indonesia
JAX Asia 2008 - Conference for Enterprise Java, SOA, Spring, Web Services, Ajax, Agile and more
BUSINESS ENTERPRISE SOLUTIONS ARCHITECTURE INFORMATION SECURITY WIRELESS & MOBILITY DATA & STORAGE DEVELOPMENT HARDWARE













News

Monday, 26 February 2007

PHP and PEAR Combined for Correct Time Between Time Zone

 

 

Melonfire over at the Builderau.com has posted a tutorial, which will teach you, how to convert temporal values between time zones with PEAR Date Class.

He begins by the possibilities that PHP provides with the time factor. He says, PHP comes with a catalog of date and time functions, designed to let you retrieve temporal information, devise it into a format you require, and either use it in a calculation or display it to the user. However, if you'd like to do something more complicated, things get much complex like displaying the time on a Web page.

He further says with PHP, to display the time on a Web page is convenient. He says, this can be done by using the date() function to read the server's clock and display the required information in a specific format. He shows how to display the time in a different location. He explains through an example, to see ‘home’ time instead of local time if your company is located in a different country from your server. He explains that to get this done you have to figure out the difference between the two places and perform some date arithmetic to adjust for the different time zones. If the time difference is significant, you need to take account of whether the new time is on the day before or after, worry about daylight savings time, and keep track of end-of-the-month and leap year constraints, he explains.

He cautions that the math to perform such time zone conversions can get complicated if you do it manually. He says, though PHP has built-in time zone functions to help with this, but these are not intuitive and require a fair amount of time to get used to. He informs you about a quicker alternative which is to use the PEAR Date class. It comes with built-in support for time zones and is, by far, the simplest way to perform these conversions, he initiates. He talks about the preliminary requirements. You have to have a working Apache and PHP installation and should find out that the PEAR Date class has been correctly installed.

He divides the main part of the tutorial in the following sections:

Getting Started

He first starts with the basics where he explains the initializing and using a Date Object. He creates a PHP Script with a code example. He gives the output of the PHP script too.

Converting Between Time Zones

After explaining the basics, he talks about time zones. He says once you have a Date() object initialized, converting from one time zone to another is a simple two-step process:

  • Tell the Date class which time zone you're converting from, with the setTZByID() method
  • Then, tell the Date class which time zone you wish to convert to, with the convertTZByID() method


Calculating GMT Offsets

Another piece of information, he gives is to work with time zones is the GMT offset— that is, the difference between the specified time zone and standard GMT. The PEAR Date class lets you get this information easily, via its getRawOffset() method. This he illustrates with an example.

Adding and Subtracting Timespans

He says that the Date class also lets you perform sophisticated date arithmetic on temporal values, adding or subtracting durations to a date/time value. These durations (or timespans) are expressed as a string containing day, hour, minute and/or second components. He shows how to carry out the process with a code example.

 

Read the Post

 
 
print save email comment

print

save

email

comment

 
 

Search SDA Asia

Free eNewsletter

SDA Asia Magazine Free Download
 
 
 
Copyright @ 2008 SDA Asia Magazine - All Right Reserved Privacy Policy | Terms of Use