PHP cross-time zone function implementation

Source: Internet
Author: User
Tags time zones codeigniter

Now there is a cross-Time Zone application. users logging on to different time zones need to view their own time zones and switch between time zones.

My idea is that all the stored time in the system is GMT (UTC) time. When a user logs on, the corresponding display is made based on the user's time zone.

First, let's take a look at how to set the time zone in PHP. PHP can be set in php. set date in ini. timezone attribute. You can call ini_set ('date. timezone ', ''). You can also use the date_default_timezone_set () function or set it in the htaccess file.

The Default Time Zone of the server. If the settings are inconsistent with the expected time zone, and we do not have the permission to modify the Global Time Zone configuration, we only need to use the code.

PHP also provides a convenient function, gmdate (), which allows us to always get the GMT time without worrying about the time zone settings of the server. My idea is to use this function.

I used the Codeigniter framework in my project. The date helper in the Framework provides several convenient functions that can be used to handle the multi-time zone situations in applications.

Here, now () always returns the current gmt time; local_to_gmt () can convert the local time to gmt; gmt_to_local () can convert the gmt time to the local time;

Consider a typical application scenario:

The current time is displayed after the user logs in. This is the time when we can use now () to get the standard gmt time, and then use the gmt_to_local () function to convert it to the time zone of the user.

The user needs to publish at a time. The user published a "18:30:00" time. We cannot directly store the time in the database. We must first use local_to_gmt () to convert the standard gmt time into the database, in this way, the time of the entire system can be consistent.

The details of these two functions are based on the time zone and then calculated accordingly. During calculation, you can also consider the start time and end time of the time zone, but you need to maintain it yourself.

Codeigniter provides a complete list of time zones. timezone_menu () can display a drop-down list of time zones, but the time in this list cannot completely correspond to the display of time zones in PHP, this is a problem in PHP, but you can use the following function to display the text of each input time zone.

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

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.