PHP Cross-zone feature implementation _php Tutorial

Source: Internet
Author: User
Tags time zones codeigniter
There is now a cross-time zone app where users who log in in different time zones need to see their time zones and also be able to switch time zones.

My idea is that all the storage time in the system is GMT (UTC) time, and when the user logs in, the corresponding display is made according to the time zone in which the user is located.

First look at how the time zone in PHP is set up. The method of setting in PHP is more flexible, you can set the Date.timezone property in PHP.ini, you can call Ini_set (' Date.timezone ', ') by code, or you can use the function date_default_ Timezone_set (), or set in the Htaccess file.

The default time zone for the server, if it does not match the time zone that we want, and we do not have permission to modify the global time zone configuration, only with the help of the code.

PHP also provides a handy function, Gmdate (), which allows us to always get GMT time without worrying about the server's time zone setting, and my idea is based on this function.

My project uses the CodeIgniter framework, and the date helper in the framework provides several handy functions that can be used to handle multi-temporal situations in your application.

where now () always returns the current time of GMT, Local_to_gmt () can convert the local time to GMT, and gmt_to_local () can convert GMT time to local time;

Consider a typical application scenario:

When the user logs in, the current time is displayed. This is where we can use now () to get the standard GMT time, and then use the gmt_to_local () function to convert to the time of the user's time zone.

The user wants to publish a time. Users posted a "2010-07-10 18:30:00" time, we can not directly into the database, we must first use the LOCAL_TO_GMT () conversion standard GMT time into the database, so as to ensure that the entire system time consistency.

The details of these two functions are in fact based on the time zone, and then the corresponding operation is obtained. When calculating, you can also consider daylight saving time, but the start and end times of your time zone will require your own maintenance.

A more complete list of time zones is available in CodeIgniter, Timezone_menu () can display a drop-down list for a time zone, but the time in this list does not correspond exactly to the time zone display in PHP, which is a problem for PHP itself, but can be done by following this function. For each time zone you enter, you can get a corresponding time zone text display.

if (! function_exists ("Tz_offset_to_name")) {/     * Takes a GMT offset (in hours) and returns a timezone name * * Fun     Ction tz_offset_to_name ($offset)     {             $offset *= 3600;//convert hour offset to seconds             $abbrarray = timezone_a Bbreviations_list ();             foreach ($abbrarray as $abbr)             {                     foreach ($abbr as $city)                     {                             if ($city [' offset '] = = $offset)                             {                                     return $city [' timezone_id '];             }} return FALSE;     } }

http://www.bkjia.com/PHPjc/752483.html www.bkjia.com true http://www.bkjia.com/PHPjc/752483.html techarticle There is now a cross-time zone app where users who log in in different time zones need to see their time zones and also be able to switch time zones. My idea is that all the storage in the system ...

  • 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.