PHP applies the solution across time zone of UTC time

  • 2020-05-27 04:29:38
  • OfStack

1. Set the internal time zone to UTC time.(UTC can also be called GMT)
PHP Settings:
date_default_timezone_set("UTC");
Yii Settings:
config/ main.php add :'timeZone'= > 'UTC',
After this setting, the generation time of PHP is almost the same as that of UTC. For example:
// output the current UTC time
date("Y-m-d H:i:s");

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

3. The time when the server sends to the front end is in UTC time format, which is converted into local time by JS for display. The internal data of JS is separated from the display data.
JS conversion function reference:
 
/** 
*  will 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 is not compatible '-' To separate the date  
// Parse string and local time assignment  
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; 
} 
/** 
*  Change the 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 '-' To separate the date  
// Parse string  
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 the date , Retain local time ( for UTC To convert ) 
utcTime.setFullYear(tmpTime.getFullYear()); 
utcTime.setMonth(tmpTime.getMonth());utcTime.setMonth(tmpTime.getMonth());//? If we do not repeat , The assignment is invalid  
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 lead 0 
function preZero(str) { 
return str.toString().length<2 ? '0'+str : str; 
} 

Related articles: