php 国际标准时间_时区-如何在PHP中获得格林威治标准时间?
这个问题启发了我,我写了一个替代函数将时间戳转换为所需的时区。
因此,CORRECT程序步骤记录,显示(转换)时间:
1) record timestamp with time()
2) If you need to get a different timezone, use:
2.1) Class based:
2.1.1) DateTime class + setTimezone
or
2.1.2) date_default_timezone_set() and then date()
2.1.1) is better and more flexible than 2.1.2)
Function:
function getLTime_class($ts, $uTZ, $format_str="Y.m.d H:i", $sTZ="America/Toronto"){
// $ts - timestamp recorded with time(); if have date => convert to timestamp: strtotime($date_input); //for ex strtotime('2012-02-17 12:00:00'); => 1345219200 - same value for summer and winter (for toronto timezone);
// $uTZ - TZ string (ex: 'America/Toronto'); convert timestamp to this TimeZone
// $sTZ - TZ string (ex: 'America/Toronto'); convert timestamp from this TimeZone
$TimeZone_server=new DateTimeZone($sTZ);
$date_obj=new DateTime("@$ts", $TimeZone_server);
$TimeZone_local=new DateTimeZone($uTZ);
$date_obj->setTimeZone($TimeZone_local);
return $date_obj->format($format_str);
}
Usage:
$date_input=$_POST['date_input']; //read from POST
$ts=strtotime($date_input); //for ex strtotime('2012-02-17 12:00:00'); => 1345219200 - same value for summer and winter (for toronto timezone);
$userTZ='America/Toronto'; //read from user params
$result=getLTime_class($ts, $userTZ, 'Y-m-d H:i:s');
2.2) Non-Class based:
Function:
//this function replaces the function with DateTime class. It's faster. $uTZoffset is integer.
function getLTime_nonclass($ts, $uTZoffset, $format_str="Y.m.d H:i"){
// $ts - timestamp recorded with time(); if have date => convert to timestamp: strtotime($date_input); //for ex strtotime('2012-02-17 12:00:00'); => 1345219200 - same value for summer and winter (for toronto timezone);
// $uTZoffset - TZ offset (integer) $uTZoffset must consider DTS (for ex: in summer $uTZoffset=-4; in winter $uTZoffset=-5)
$ts_offset=date('Z',$ts);
if ($uTZoffset) $add_offset=$ts_offset-date('Z'); //when not UTC
else $add_offset=0; //when UTC
return date($format_str,$ts-$ts_offset+$uTZoffset*3600+$add_offset);
}
Usage:
$date_input=$_POST['date_input']; //read from POST
$ts=strtotime($date_input); //for ex strtotime('2012-02-17 12:00:00'); => 1345219200 - same value for summer and winter (for toronto timezone);
$uTZoffset=-4; //read from user params. $uTZoffset - TZ offset (integer) $uTZoffset must consider DTS (for ex: in summer $uTZoffset=-4; in winter $uTZoffset=-5)
$result=getLTime_nonclass($ts, $uTZoffset, 'Y-m-d H:i:s');
结果:
$ date_input = 2012-08-17 12:00:00
Server date: summer (2013-08-26)
getLTime_class => $userTZ='America/Toronto' => 2012-08-17 12:00:00
getLTime_nonclass => $uTZoffset=-4 => 2012-08-17 12:00:00
getLTime_class => $userTZ='UTC' => 2012-08-17 16:00:00
getLTime_nonclass => $uTZoffset=0 => 2012-08-17 16:00:00
Server date: winter (2013-02-26)
getLTime_class => $userTZ='America/Toronto' => 2012-08-17 12:00:00
getLTime_nonclass => $uTZoffset=-5 => 2012-08-17 12:00:00
getLTime_class => $userTZ='UTC' => 2012-08-17 16:00:00
getLTime_nonclass => $uTZoffset=0 => 2012-08-17 16:00:00
$ date_input = 2012-02-17 12:00:00
Server date: summer (2013-08-26)
getLTime_class => $userTZ='America/Toronto' => 2012-02-17 12:00:00
getLTime_nonclass => $uTZoffset=-4 => 2012-02-17 12:00:00
getLTime_class => $userTZ='UTC' => 2012-02-17 17:00:00
getLTime_nonclass => $uTZoffset=0 => 2012-02-17 17:00:00
Server date: winter (2013-02-26)
getLTime_class => $userTZ='America/Toronto' => 2012-02-17 12:00:00
getLTime_nonclass => $uTZoffset=-5 => 2012-02-17 12:00:00
getLTime_class => $userTZ='UTC' => 2012-02-17 17:00:00
getLTime_nonclass => $uTZoffset=0 => 2012-02-17 17:00:00
我决定使用getLTime_nonclass希望比使用类更快地进行转换。我要问的是确认getLTime_nonclass是getLTime_class的有效替代品。请随时发表您的意见。 谢谢
php 国际标准时间_时区-如何在PHP中获得格林威治标准时间?相关推荐
- python中换行符怎么写_语法 - 如何在Python中执行换行符(换行符)?
语法 - 如何在Python中执行换行符(换行符)? 我有一长串代码,我希望在多行之间分解. 我使用什么,语法是什么? 例如,添加一串字符串, e = 'a' + 'b' + 'c' + 'd' 并将 ...
- java 服务器发布_我如何在java中发布到服务器?
我想通过流向服务器发送一个字符串"hello world". 到目前为止我做了: private void PostData() { HttpURLConnection urlCo ...
- python按日期排序_你如何在python中获得按创建日期排序的目录列表?
jfs.. 55 这是一个更详细@Greg Hewgill的答案.它最符合问题要求.它区分了创建和修改日期(至少在Windows上). #!/usr/bin/env python from stat ...
- 背景图层和普通图层的区别_新手如何在PS中创建图层?不容错过的7种方法,你值得学习...
昨天跟小波一起认识了PS图层的童鞋应该已经对它不陌生了,那么在了解之后就要来实践操作,毕竟实践出真知嘛.那这一章就一起来学习在PS中创建图层吧. 在PS中,图层的创建方法有很多种,包括在"图 ...
- 中input怎么接受后台传值_[vue3]如何在vue3中优雅地使用vmodel?
Vue中的数据绑定 绑定数据有三种方式: 插值,也就是{{name}}的形式,以文本的形式和实例data中对应的属性进行绑定 v-bind v-model v-bind eg:v-bind:class ...
- java创建单线程计时器_我们如何在Java中实现计时器线程?
该定时器类计划任务一次或多次给定的时间运行.它也可以作为后台程序线程在后台运行.要将Timer与守护程序线程相关联,有一个带有布尔值的构造函数.计时器以固定的延迟和固定的速率安排任务.在固定的延迟中, ...
- mysql获取一个表的数据作为值插入_请问如何在mysql中得到一个即将插入数据表中的那条数据的id值(id自增长)?...
我们在写数据库程序的时候,经常会需要获取某个表中的最大序号数, 一般情况下获取刚插入的数据的id,使用select max(id) from table 是可以的. 但在多线程情况下,就不行了. 下面 ...
- java 初始化参数_我们如何在Java中的对象参数中初始化数组?
您可以使用构造函数或使用setter方法来初始化与其他任何值一样在类内部声明的数组变量. 示例 在下面的Java示例中,我们声明一个数组类型的实例变量,并从构造函数中对其进行初始化.public cl ...
- 编写一个java程序_鼠标在java窗口上的坐标_请问如何在Java中获取窗口外部的鼠标单击坐标...
尽管可能,但可能会受到限制: 为焦点事件添加一个AWTEventListener.只要您的应用在单击按钮之前就具有焦点,就会收到焦点丢失事件.然后查询指针位置. 限制是,当然,您的应用程序失去了焦点. ...
- python画rgb渐变色_请问如何在matplotlib中画出自定义渐变色?
算了,自问自答一下,用matplotlib.colors里面的LinearSegmentedColormap类可以自定义color_map. 代码: import numpy as np import ...
最新文章
- AI Time | 激辩自动驾驶:近在咫尺还是遥遥无期
- Linux 基础 - 磁盘管理 -06
- 使用分层实现业务处理(二)
- 用 Linux 和 Apache Hadoop 进行云计算
- Eclipse下搭建C语言开发环境
- 顺序表的插入删除查找遍历
- Linux 系统创建.sh文件以及赋权、执行
- 40 张图揭秘「键入网址发生了什么」
- Mac 本地搭建服务器实现itms-services方式安装ipa(自制证书)
- kali-beef工具(xss-stored)
- 机器学习之利用线性回归预测波士顿房价和可视化分析影响房价因素实战(python实现 附源码 超详细)
- ROS 基础知识(一)
- [VB.NET]雪花飘的屏保
- 算法:什么是宠物收养所问题?
- Qt编写水波进度条控件
- Java生成“年月日“+流水号
- 金工如何运用计算机思维,金工的实习心得体会
- error: command ‘gcc‘ failed: No such file or directory
- 天宇优配|A股迎来“重要转折” 私募机构投资信心与仓位同步走高
- 温室大棚控制系统智能轻松种菜