How to use PHP and pear for different time zone conversions

Source: Internet
Author: User
Tags date contains getdate time zones local time pear php and php code

PHP has a series of date and time functions, which facilitates you to get time information that you can convert into the format you want and use to calculate or present to your users. But if you want to implement some complex functionality, things can become very complex.

A simple example is to display the time on a Web page. In PHP, you can simply use the data () function to read the server's clock and display it in the specified format, but if you want to display a different time zone for example, your company and server are in different countries, you need to see local time instead of local time.

Therefore, you need to calculate the time difference between the two places and make some calculations to make adjustments in different timezone, and if the slack is important, you also need to take into account the date change, daylight saving time, last day of the month, and the limits of a leap year, and so on.

As you can imagine, if you manually complete the mathematical calculations of these time zones, this will soon become a very complex thing. Fortunately, the time zone functions built in PHP can help solve this problem, but these functions are not intuitive and require some time to familiarize themselves with their use. A quicker option, of course, is to use the Date class library of pear, which has built-in support for time zones, and so far it is the easiest way to do time zone conversions.

This guide will show you how to use the date class of pear to make different time numeric conversions in different timezone. I assume that you have installed the date class libraries for Apache, PHP, and pear and that they are working correctly.

Note: You can install Pear's date class library directly from the Internet, and you can download or use the guidance provided by it for installation.

Begin

Let's start with the basics of initializing and using the Date object, and create the PHP code in List A:

<?php
Include class
Include ("date.php");

Initialize Object
$d = new Date ("2006-06-21 15:45:27");

Retrieve date
echo $d->getdate ();
?>

This example is very simple, the class code it contains uses a date/time string to initialize a day () object, and then uses the GETDATE () method to show the value you inserted, following is the result of the output:

2006-06-21 15:45:27

How do I display a date in a different format? If it is a standard format, such as the ISO format, it is only necessary to pass a modifier to GETDATE () to indicate that the code is as shown in Listing B:

<?php
Include class
Include ("date.php");

Initialize Object
$d = new Date ("2006-06-21 15:45:27");

Retrieve date as Timestamp
echo $d->getdate (Date_format_iso_basic);
?>

The output of this example conforms to the standard ISO format.

20060621t154527z

If you need a custom format, you can use the format () method to do this, as in PHP's original date () function, the format () method can receive a series of format definitions to indicate the exact format of each part of the date. Listing C shows an example of using the format () method (you can refer to the class documentation to get a complete list of format definitions).

<?php
Include class
Include ("date.php");

Initialize Object
$d = new Date ("2006-06-21 15:45:27");

Retrieve date as formatted string
echo $d->format ("%A,%d%B%Y%T");
?>

The results of the output are as follows:

Wednesday June 2006 15:45:27

Converting between time zones

Now that you know the basics of how to use it, let's discuss the time zone, and once you have the initialized date () object, the transition time zone's work can be divided into two simple steps:

Use the Settzbyid () method to tell the date class which time zone to convert from;
Then, use the Converttzbyid () method to tell the date class the target time zone you are converting.


List D shows this part of the code:

<?php
Include class
Include ("date.php");

Initialize Object
$d = new Date ("2006-06-21 10:36:27");

Set local time zone
$d->settzbyid ("GMT");

Convert to foreign time zone
$d->converttzbyid ("IST");

Retrieve converted Date/time
echo $d->format ("%A,%d%B%Y%T");
?>

In this example, I convert Greenwich Mean Time (GMT) to India Standard Time (IST), India time is 5.5 hours earlier than GMT, so the result of this script output is:

Wednesday June 2006 16:06:27

Is it simple? The following table E example shows the problem if you are dealing with leap years and end dates:

<?php
Include class
Include ("date.php");

Initialize Object
$d = new Date ("2008-03-01 06:36:27");

Set local time zone
$d->settzbyid ("GMT");

Print local time
echo "local". $d->format ("%A,%d%B%Y%T"). "\ n";

Convert to foreign time zone
$d->converttzbyid ("PST");

Retrieve converted Date/time
echo "Destination time is". $d->format ("%A,%d%B%Y%T");
?>

The output is as follows:

Local Saturday, March 2008 06:36:27

Destination Time is Friday February 2008 22:36:27

Note: You may be wondering where the time zone ID comes from, in fact, you can find a complete list of time zone IDs in your class documentation.

Calculate Greenwich Mean Time shift

Another efficient method is to use Greenwich Mean time displacement to convert the time zone, that is, by using the difference between the specified time zone and Greenwich Mean time, which is facilitated by the date class of pear, which can be achieved by the Getrawoffset () method, The list f shows an example.

<?php
Include class
Include ("date.php");

Initialize Object
$d = new Date ("2006-06-21 10:36:27");

Set local time zone
$d->settzbyid ("PST");

Get raw offset from GMT, in msec
echo $d->tz->getrawoffset ();
?>

Here, the Getrawoffset () method calculates the difference between the local time and Greenwich Mean time, and the output results are as follows:

-28800000

Note that the amount of displacement is computed in milliseconds, so you need to divide it by 3600000 (the number of milliseconds per hour) to get the number of hours that the interval varies.

Tip: You can use the Indaylighttime () to see if the target time zone is in daylight savings, and for more information about this method in the class library document.

Adding and reducing time spans

You can perform complex date calculations on time values by using the Date class library, adding or decreasing date/time values, which can be represented as a string component that contains days, hours, minutes, and/or seconds, and list G shows a simple example.

<?php
Include class
Include ("date.php");

Initialize Object
$d = new Date ("2006-06-21 15:45:27");

Add 01:20 to it
$d->addspan (New Date_span ("0,1,20,0"));

Retrieve date as formatted string
echo $d->format ("%A,%d%B%Y%T");
?>

In this example, by calling the Addspan () method and the Date_span () object of the date class, I added 1 hours and 20 minutes to the initial timestamp, and the output was obvious:

Wednesday June 2006 17:05:27

As with adding a time span, you can use subtraction, which is, in fact, the purpose of the Subtractspan () method, and listing H shows how to use it.

<?php
Include class
Include ("date.php");

Initialize Object
$d = new Date ("2006-06-21 15:45:27");

Add 01:20 to it
$d->addspan (New Date_span ("0,1,20,0"));

Subtract 00:05 from it
$d->subtractspan (New Date_span ("0,0,5,0"));

Retrieve date as formatted string
echo $d->format ("%A,%d%B%Y%T");
?>

In this example, I first added 1 hours and 20 minutes, then minus 5 minutes, the final result is added 1 hours and 15 minutes, the output is as follows:

Wednesday June 2006 17:00:27

As these examples show, pear's date class library provides an intuitive and efficient way to perform complex date calculations, and if you are looking for an easy way to convert timestamps in different locations, I recommend this approach enthusiastically.



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.