PHP跨時(shí)區(qū)(UTC時(shí)間)應(yīng)用解決方案
更新時(shí)間:2013年01月11日 11:49:15 作者:
解決PHP跨時(shí)區(qū)應(yīng)用需要將將程序內(nèi)部時(shí)區(qū)設(shè)置為UTC時(shí)間.(UTC 也可以叫 GMT) 數(shù)據(jù)庫(kù)中存儲(chǔ)UTC時(shí)間等等,感興趣的朋友可以了解下
1.將程序內(nèi)部時(shí)區(qū)設(shè)置為UTC時(shí)間.(UTC 也可以叫 GMT)
PHP設(shè)置:
date_default_timezone_set("UTC");
Yii設(shè)置:
config/main.php 中添加 :'timeZone'=>'UTC',
如此設(shè)置后,PHP生成的時(shí)間基本都是UTC時(shí)間了.例如:
//輸出當(dāng)前UTC時(shí)間
date("Y-m-d H:i:s");
2.數(shù)據(jù)庫(kù)中存儲(chǔ)UTC時(shí)間.
可以用PHP控制,也可以通過設(shè)置數(shù)據(jù)庫(kù)時(shí)區(qū)來實(shí)現(xiàn).
3.服務(wù)端發(fā)送到前端的時(shí)間均為UTC時(shí)間格式, 由JS將其轉(zhuǎn)換為本地時(shí)間后進(jìn)行顯示.JS內(nèi)部數(shù)據(jù)與顯示數(shù)據(jù)分離.
JS轉(zhuǎn)換函數(shù)參考:
/**
* 將UTC時(shí)間轉(zhuǎn)為本地時(shí)間
* @param string utcTime utc時(shí)間字符串 格式 :'Y-m-d H:i:s'
* @return string 本地時(shí)間字符串 格式 :'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時(shí)間對(duì)象
utcTime=utcTime.replace("-", "/").replace("-", "/"); //火狐不兼容'-'分隔日期
//解析字符串及本地時(shí)間賦值
locTime.setTime(Date.parse(utcTime)-locTime.getTimezoneOffset()*60000);
//本地時(shí)間字符串格式化
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;
}
/**
* 將本地時(shí)間轉(zhuǎn)為UTC時(shí)間
* @param string locTime utc時(shí)間字符串 格式 :'Y-m-d H:i:s'
* @return string 本地時(shí)間字符串 格式 :'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("-", "/"); //火狐不兼容'-'分隔日期
//解析字符串
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 {
//設(shè)置日期,保留本地時(shí)間(供UTC轉(zhuǎn)換用)
utcTime.setFullYear(tmpTime.getFullYear());
utcTime.setMonth(tmpTime.getMonth());utcTime.setMonth(tmpTime.getMonth());//?若不重復(fù),則賦值無效
utcTime.setDate(tmpTime.getDate());
var year = utcTime.getUTCFullYear();
var month = preZero(utcTime.getUTCMonth()+1);
var date = preZero(utcTime.getUTCDate());
return year+'-'+month+'-'+date;
}
}
//單個(gè)數(shù)字添加前導(dǎo)0
function preZero(str) {
return str.toString().length<2 ? '0'+str : str;
}
PHP設(shè)置:
date_default_timezone_set("UTC");
Yii設(shè)置:
config/main.php 中添加 :'timeZone'=>'UTC',
如此設(shè)置后,PHP生成的時(shí)間基本都是UTC時(shí)間了.例如:
//輸出當(dāng)前UTC時(shí)間
date("Y-m-d H:i:s");
2.數(shù)據(jù)庫(kù)中存儲(chǔ)UTC時(shí)間.
可以用PHP控制,也可以通過設(shè)置數(shù)據(jù)庫(kù)時(shí)區(qū)來實(shí)現(xiàn).
3.服務(wù)端發(fā)送到前端的時(shí)間均為UTC時(shí)間格式, 由JS將其轉(zhuǎn)換為本地時(shí)間后進(jìn)行顯示.JS內(nèi)部數(shù)據(jù)與顯示數(shù)據(jù)分離.
JS轉(zhuǎn)換函數(shù)參考:
復(fù)制代碼 代碼如下:
/**
* 將UTC時(shí)間轉(zhuǎn)為本地時(shí)間
* @param string utcTime utc時(shí)間字符串 格式 :'Y-m-d H:i:s'
* @return string 本地時(shí)間字符串 格式 :'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時(shí)間對(duì)象
utcTime=utcTime.replace("-", "/").replace("-", "/"); //火狐不兼容'-'分隔日期
//解析字符串及本地時(shí)間賦值
locTime.setTime(Date.parse(utcTime)-locTime.getTimezoneOffset()*60000);
//本地時(shí)間字符串格式化
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;
}
/**
* 將本地時(shí)間轉(zhuǎn)為UTC時(shí)間
* @param string locTime utc時(shí)間字符串 格式 :'Y-m-d H:i:s'
* @return string 本地時(shí)間字符串 格式 :'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("-", "/"); //火狐不兼容'-'分隔日期
//解析字符串
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 {
//設(shè)置日期,保留本地時(shí)間(供UTC轉(zhuǎn)換用)
utcTime.setFullYear(tmpTime.getFullYear());
utcTime.setMonth(tmpTime.getMonth());utcTime.setMonth(tmpTime.getMonth());//?若不重復(fù),則賦值無效
utcTime.setDate(tmpTime.getDate());
var year = utcTime.getUTCFullYear();
var month = preZero(utcTime.getUTCMonth()+1);
var date = preZero(utcTime.getUTCDate());
return year+'-'+month+'-'+date;
}
}
//單個(gè)數(shù)字添加前導(dǎo)0
function preZero(str) {
return str.toString().length<2 ? '0'+str : str;
}
您可能感興趣的文章:
- 關(guān)于php程序報(bào)date()警告的處理(date_default_timezone_set)
- PHP中設(shè)置時(shí)區(qū)方法小結(jié)
- PHP中遇到的時(shí)區(qū)問題解決方法
- PHP獲取中國(guó)時(shí)間(上海時(shí)區(qū)時(shí)間)及美國(guó)時(shí)間的方法
- php時(shí)區(qū)轉(zhuǎn)換轉(zhuǎn)換函數(shù)
- php輸出全球各個(gè)時(shí)區(qū)列表的方法
- 有關(guān) PHP 和 MySQL 時(shí)區(qū)的一點(diǎn)總結(jié)
- PHP中設(shè)置時(shí)區(qū),記錄日志文件的實(shí)現(xiàn)代碼
- PHP應(yīng)用跨時(shí)區(qū)功能的實(shí)現(xiàn)方法
- PHP date_default_timezone_set()設(shè)置時(shí)區(qū)操作實(shí)例分析
相關(guān)文章
php中echo、print和print_r的區(qū)別點(diǎn)及用法總結(jié)
在本篇文章里小編給大家整理的是一篇關(guān)于php中echo、print和print_r的區(qū)別點(diǎn)及用法總結(jié)內(nèi)容,有需要的朋友們可以學(xué)習(xí)下。2021-12-12PHP函數(shù)checkdnsrr用法詳解(Windows平臺(tái)用法)
這篇文章主要介紹了PHP函數(shù)checkdnsrr用法,分析講解了在Windows平臺(tái)使用checkdnsrr函數(shù)的方法,需要的朋友可以參考下2016-03-03linux使用crontab實(shí)現(xiàn)PHP執(zhí)行計(jì)劃定時(shí)任務(wù)
前幾天寫過一篇文章,利用單純的php實(shí)現(xiàn)定時(shí)執(zhí)行任務(wù),但是效率不佳,對(duì)于linux來說用crontab實(shí)現(xiàn)更加合理2014-05-05PHP 如何利用phpexcel導(dǎo)入數(shù)據(jù)庫(kù)
以下是對(duì)PHP中利用phpexcel導(dǎo)入數(shù)據(jù)庫(kù)的實(shí)現(xiàn)代碼進(jìn)行了介紹,需要的朋友可以過來參考下2013-08-08