PHP Cross-time zone (UTC time) application Solution _php Tutorial

Source: Internet
Author: User
Tags yii
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 ...

  • 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.