1. Set the program's internal time zone to UTC time. (UTC can also be called GMT)
PHP Settings:
Date_default_timezone_set ("UTC");
Yii settings:
Added in config/main.php: ' timeZone ' = ' UTC ',
When this is set, the time that PHP generates is basically UTC time. For example:
Output current UTC time
Date ("y-m-d h:i:s");
2. UTC time is stored in the database.
It can be controlled in PHP or by setting the database time zone.
3. The time that the server sends to the front end is in UTC time Format, which is displayed after it is converted to local time by JS. js internal data is separated from the display data.
JS Conversion Function Reference:
Copy CodeThe code is as follows:
/**
* Convert UTC time to local time
* @param string utctime UTC time string format: ' y-m-d h:i:s '
* @return string local time string format: ' y-m-d h:i:s '
*/
function Utctolocal (utctime) {
if (utctime=== ' 0000-00-00 00:00:00 ' | | utctime===null | | utctime=== ' | | utctime===undefined)
return utctime;
var loctime = new Date (); Local time Object
Utctime=utctime.replace ("-", "/"). Replace ("-", "/"); Firefox incompatible '-' separation date
parsing strings and local time assignments
Loctime.settime (Date.parse (utctime)-loctime.gettimezoneoffset () *60000);
local time string formatting
var year = Loctime.getfullyear ();
var month = Prezero (Loctime.getmonth () +1);
var date = Prezero (Loctime.getdate ());
var hour = Prezero (Loctime.gethours ());
var minute = Prezero (Loctime.getminutes ());
var second = Prezero (Loctime.getseconds ());
Return year+ '-' +month+ '-' +date+ ' +hour+ ': ' +minute+ ': ' +second;
}
/**
* Convert local time to UTC time
* @param string loctime UTC time string format: ' y-m-d h:i:s '
* @return string local time string format: ' y-m-d h:i:s '
*/
function Localtoutc (loctime) {
if (loctime=== ' 0000-00-00 00:00:00 ' | | loctime=== ' 0000-00-00 ' | | loctime===null | | loctime=== "| | loctime===undefined)
return loctime;
var tmptime = new Date ();
var utctime = new Date ();
Loctime=loctime.replace ("-", "/"). Replace ("-", "/"); Firefox incompatible '-' separation date
Parsing strings
Tmptime.settime (Date.parse (loctime));
if (loctime.length>10) {
var year = Tmptime.getutcfullyear ();
var month = Prezero (Tmptime.getutcmonth () +1);
var date = Prezero (Tmptime.getutcdate ());
var hour = Prezero (Tmptime.getutchours ());
var minute = Prezero (Tmptime.getutcminutes ());
var second = Prezero (Tmptime.getutcseconds ());
Return year+ '-' +month+ '-' +date + ' +hour+ ': ' +minute+ ': ' +second;
} else {
Set date, local time reserved (for UTC conversion)
Utctime.setfullyear (Tmptime.getfullyear ());
Utctime.setmonth (Tmptime.getmonth ()); Utctime.setmonth (Tmptime.getmonth ());//? assignment is not valid if not duplicated
Utctime.setdate (Tmptime.getdate ());
var year = Utctime.getutcfullyear ();
var month = Prezero (Utctime.getutcmonth () +1);
var date = Prezero (Utctime.getutcdate ());
Return year+ '-' +month+ '-' +date;
}
}
A single number adds a leading 0
function Prezero (str) {
Return str.tostring (). length<2? ' 0 ' +str:str;
}
http://www.bkjia.com/PHPjc/326372.html www.bkjia.com true http://www.bkjia.com/PHPjc/326372.html techarticle 1. Set the program's internal time zone to UTC time. (UTC can also be called GMT) PHP Settings: Date_default_timezone_set ("UTC"); Yii settings: config/main.php added: ' TimeZone ' = ' UTC ', so set after ...