PHP Application Solution _php technique across Time zone (UTC time)

Source: Internet
Author: User
Tags getdate local time parse string string format
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:
Added in config/main.php: ' TimeZone ' => ' UTC ',
After this setting, the PHP generation time is basically UTC time. For example:
Output current UTC time
Date ("y-m-d h:i:s");

2. Storing UTC time in database.
You can use PHP control, or you can set the database time zone to implement it.

3. The time that the server sends to the front end is in UTC time Format, which is displayed by JS when it is converted to local time. JS internal data is separated from display data.
JS Conversion Function Reference:
Copy Code code 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 '-' separator 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;
}
/**
* 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 '-' separator 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 date, keep local time (for UTC conversion)
Utctime.setfullyear (Tmptime.getfullyear ());
Utctime.setmonth (Tmptime.getmonth ()); Utctime.setmonth (Tmptime.getmonth ()); If not repeated, 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;
}
}
Single Digit adds leading 0
function Prezero (str) {
Return str.tostring (). length<2? ' 0 ' +str:str;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.