How to use PHP and pear for different time zone conversions

Source: Internet
Author: User
Tags 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, if the jet lag is important, you also need to take into account the date change, daylight saving time, the last day of the month and the limits of leap years, 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? In the case of 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

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.