Home >Backend Development >PHP Tutorial >PHP cross-time zone (UTC time) application solution_PHP tutorial

PHP cross-time zone (UTC time) application solution_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:14:03836browse

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 Add: 'timeZone'=>'UTC' to /main.php,
After this setting, the time generated by PHP is basically UTC time. For example:
//Output the current UTC time
date ("Y-m-d H:i:s");

2. Store UTC time in the database .
can be controlled with PHP or by setting the database time zone.

3. The time sent by the server to the front-end is in UTC time format , which is converted to local time by JS and then displayed. JS internal data is separated from the display data.
JS conversion Function reference:

Copy code The 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 with '-' separated date
//Parse string and local time assignment
locTime.setTime(Date.parse(utcTime)-locTime.getTimezoneOffset()*60000);
//Local time character 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 is not compatible with '-' separated dates
//Parse string
tmpTime.setTime(Date.parse(locTime));
if(locTime. length>10) {
var year = tmpTime.getUTCFulYear();
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 the date and keep the local time (for UTC conversion)
utcTime.setFullYear(tmpTime.getFullYear());
utcTime.setMonth(tmpTime.getMonth()); utcTime.setMonth(tmpTime.getMonth());//?If it is not repeated, the assignment is invalid
utcTime.setDate(tmpTime.getDate());
var year = utcTime.getUTCFulYear();
var month = preZero(utcTime.getUTCMonth()+1);
var date = preZero(utcTime. getUTCDate());
return year+'-'+month+'-'+date;
}
}
//Add leading 0 to a single number
function preZero(str) {
return str.toString().length<2 ? '0'+str : str;
}

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/326372.htmlTechArticle1. 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: Add: 'timeZone'='UTC' in config/main.php, after setting like this...
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