To solve the problem of PHP cross-time zone, you need to set the internal time zone of the program to UTC time. (UTC can also be called GMT) and store the UTC time in the database. if you are interested, you can understand
1. set the internal time zone of the program to UTC time. (UTC can also be called GMT)
PHP settings:
Date_default_timezone_set ("UTC ");
Yii settings:
Add 'timezone '=> 'utc' to config/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 the UTC time in the database.
You can use PHP or set the database time zone.
3. the time that the server sends to the front end is in UTC time format.. JS converts it to the local time for display. JS internal data and display data separation.
JS conversion function reference:
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 = '2014-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 '-' separator date
// Parse string and local time assignment
LocTime. setTime (Date. parse (utcTime)-locTime. getTimezoneOffset () * 60000 );
// Format the local time string
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 = '2014-00-00 00:00:00 '| locTime = '2014-00-00' | locTime = null | locTime ='' | locTime = undefined)
Return locTime;
Var tmpTime = new Date ();
Var utcTime = new Date ();
LocTime = locTime. replace ("-", "/"). replace ("-", "/"); // Firefox incompatible '-' separator date
// Parse the 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 and retain the local time (for UTC conversion)
UtcTime. setFullYear (tmpTime. getFullYear ());
UtcTime. setMonth (tmpTime. getMonth (); utcTime. setMonth (tmpTime. getMonth ());//? If no duplicate values exist, the value 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;
}
}
// Add the leading 0 to a single number
Function preZero (str ){
Return str. toString (). length <2? '0' + str: str;
}