Home  >  Article  >  Backend Development  >  PHP cross-time zone function implementation_PHP tutorial

PHP cross-time zone function implementation_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:33:27765browse

There is now a cross-time zone application. Users logging in from different time zones need to see the time in their own time zone and be able to switch time zones.

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

First, let’s learn how to set the time zone in PHP. The setting methods in PHP are more flexible and diverse. You can set the date.timezone attribute in php.ini, call ini_set('date.timezone', '') through code, or use the function date_default_timezone_set(), or in htaccess file settings.

If the default time zone of the server does not match the time zone we want, and we do not have permission to modify the global time zone configuration, we can only use code.

PHP also provides a convenient function, gmdate(), which allows us to always get the GMT time without caring about the server's time zone setting. My idea is based on this function.

I use the Codeigniter framework in my project. The date helper in the framework provides several convenient functions that can be used to handle multiple time zones in applications.

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

Consider a typical application scenario:

After the user logs in, the current time must be displayed. This is where we can use now() to get the standard gmt time, and then use the gmt_to_local() function to convert it to the time in the user's time zone.

The user wants to post a time. The user published a time of "2010-07-10 18:30:00". We cannot directly store it in the database. We must first use local_to_gmt() to convert the standard gmt time and store it in the database. This can ensure that the time is maintained in the entire system. consistent.

The details of these two functions are actually obtained by performing corresponding operations based on the time zone. Daylight saving time can also be considered when calculating, but the start and end times of daylight saving time in the local time zone need to be maintained by yourself.

codeigniter provides a relatively complete time zone list. timezone_menu() can display a drop-down list of time zones, but the time in this list cannot completely correspond to the time zone display that comes with PHP. This is a problem with PHP itself. , but you can use the following function to get a corresponding time zone text display for each time zone entered.

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; 
    } 
}

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/752483.htmlTechArticleThere is now a cross-time zone application. Users logging in from different time zones need to see the time in their own time zone, and at the same time Ability to switch time zones. My idea is that all stored in the system...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn