php如何设置utc时区

发布时间:2023-09-05 点击:94
php设置utc时区的方法:1、通过“date_default_timezone_set("utc");”方法将程序内部时区设置为utc时间;2、使用“localtoutc”方法将本地时间转为utc时间。
推荐:《php视频教程》
php跨时区(utc时间)设置的方法
本文介绍下,在php中跨时区设置的方法,将程序内部时区设置为utc时间.(utc 也可以叫 gmt) 数据库中存储utc时间等,供大家做个参考。
php跨时区(utc时间)设置
1,将程序内部时区设置为utc时间.(utc 也可以叫 gmt)
php设置:
代码示例:
date_default_timezone_set("utc");yii设置:
config/main.php 中添加 :'timezone'=>'utc',如此设置后,hp生成的时间基本都是utc时间了。
例如:
代码示例:
//输出当前utc时间date("y-m-d h:i:s");2,数据库中存储utc时间.
可以用php控制,也可以通过设置数据库时区来实现.
2,服务端发送到前端的时间均为utc时间格式, 由js将其转换为本地时间后进行显示。
js内部数据与显示数据分离。
js转换函数:
代码示例:
/* 将utc时间转为本地时间* @param string utctime utc时间字符串 格式 :'y-m-d h:i:s'* @return string 本地时间字符串 格式 :'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时间对象utctime=utctime.replace("-", "/").replace("-", "/"); //火狐不兼容'-'分隔日期//解析字符串及本地时间赋值loctime.settime(date.parse(utctime)-loctime.gettimezoneoffset()*60000);//本地时间字符串格式化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;}/* 将本地时间转为utc时间* @param string loctime utc时间字符串 格式 :'y-m-d h:i:s'* @return string 本地时间字符串 格式 :'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 {//设置日期,保留本地时间(供utc转换用)utctime.setfullyear(tmptime.getfullyear());utctime.setmonth(tmptime.getmonth());utctime.setmonth(tmptime.getmonth());//?若不重复,则赋值无效utctime.setdate(tmptime.getdate());var year = utctime.getutcfullyear();var month = prezero(utctime.getutcmonth() 1);var date = prezero(utctime.getutcdate());return year '-' month '-' date;}}//单个数字添加前导0function prezero(str) {return str.tostring().length<2 ? '0' str : str;}

网站打开慢有时候还打不开请处理
日本动态拨号云有什么用?
以下三条并不是非法信息-云服务器问题
游戏服务器的配置应该如何选择?
云服务器搭建网站一键脚本
新道B轮融资成热点 网站域名涵义被误解
AD-新建组命令 New-ADGroup
网线如何连接路由器 网线怎样连接路由器