PHP cross-Time Zone (UTC time) application solution

Source: Internet
Author: User
Tags parse string

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: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 = '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;
}

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.