这个问题启发了我,我写了一个替代函数将时间戳转换为所需的时区。

因此,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中获得格林威治标准时间?相关推荐

  1. python中换行符怎么写_语法 - 如何在Python中执行换行符(换行符)?

    语法 - 如何在Python中执行换行符(换行符)? 我有一长串代码,我希望在多行之间分解. 我使用什么,语法是什么? 例如,添加一串字符串, e = 'a' + 'b' + 'c' + 'd' 并将 ...

  2. java 服务器发布_我如何在java中发布到服务器?

    我想通过流向服务器发送一个字符串"hello world". 到目前为止我做了: private void PostData() { HttpURLConnection urlCo ...

  3. python按日期排序_你如何在python中获得按创建日期排序的目录列表?

    jfs.. 55 这是一个更详细@Greg Hewgill的答案.它最符合问题要求.它区分了创建和修改日期(至少在Windows上). #!/usr/bin/env python from stat ...

  4. 背景图层和普通图层的区别_新手如何在PS中创建图层?不容错过的7种方法,你值得学习...

    昨天跟小波一起认识了PS图层的童鞋应该已经对它不陌生了,那么在了解之后就要来实践操作,毕竟实践出真知嘛.那这一章就一起来学习在PS中创建图层吧. 在PS中,图层的创建方法有很多种,包括在"图 ...

  5. 中input怎么接受后台传值_[vue3]如何在vue3中优雅地使用vmodel?

    Vue中的数据绑定 绑定数据有三种方式: 插值,也就是{{name}}的形式,以文本的形式和实例data中对应的属性进行绑定 v-bind v-model v-bind eg:v-bind:class ...

  6. java创建单线程计时器_我们如何在Java中实现计时器线程?

    该定时器类计划任务一次或多次给定的时间运行.它也可以作为后台程序线程在后台运行.要将Timer与守护程序线程相关联,有一个带有布尔值的构造函数.计时器以固定的延迟和固定的速率安排任务.在固定的延迟中, ...

  7. mysql获取一个表的数据作为值插入_请问如何在mysql中得到一个即将插入数据表中的那条数据的id值(id自增长)?...

    我们在写数据库程序的时候,经常会需要获取某个表中的最大序号数, 一般情况下获取刚插入的数据的id,使用select max(id) from table 是可以的. 但在多线程情况下,就不行了. 下面 ...

  8. java 初始化参数_我们如何在Java中的对象参数中初始化数组?

    您可以使用构造函数或使用setter方法来初始化与其他任何值一样在类内部声明的数组变量. 示例 在下面的Java示例中,我们声明一个数组类型的实例变量,并从构造函数中对其进行初始化.public cl ...

  9. 编写一个java程序_鼠标在java窗口上的坐标_请问如何在Java中获取窗口外部的鼠标单击坐标...

    尽管可能,但可能会受到限制: 为焦点事件添加一个AWTEventListener.只要您的应用在单击按钮之前就具有焦点,就会收到焦点丢失事件.然后查询指针位置. 限制是,当然,您的应用程序失去了焦点. ...

  10. python画rgb渐变色_请问如何在matplotlib中画出自定义渐变色?

    算了,自问自答一下,用matplotlib.colors里面的LinearSegmentedColormap类可以自定义color_map. 代码: import numpy as np import ...

最新文章

  1. AI Time | 激辩自动驾驶:近在咫尺还是遥遥无期
  2. Linux 基础 - 磁盘管理 -06
  3. 使用分层实现业务处理(二)
  4. 用 Linux 和 Apache Hadoop 进行云计算
  5. Eclipse下搭建C语言开发环境
  6. 顺序表的插入删除查找遍历
  7. Linux 系统创建.sh文件以及赋权、执行
  8. 40 张图揭秘「键入网址发生了什么」
  9. Mac 本地搭建服务器实现itms-services方式安装ipa(自制证书)
  10. kali-beef工具(xss-stored)
  11. 机器学习之利用线性回归预测波士顿房价和可视化分析影响房价因素实战(python实现 附源码 超详细)
  12. ROS 基础知识(一)
  13. [VB.NET]雪花飘的屏保
  14. 算法:什么是宠物收养所问题?
  15. Qt编写水波进度条控件
  16. Java生成“年月日“+流水号
  17. 金工如何运用计算机思维,金工的实习心得体会
  18. error: command ‘gcc‘ failed: No such file or directory
  19. 天宇优配|A股迎来“重要转折” 私募机构投资信心与仓位同步走高
  20. 温室大棚控制系统智能轻松种菜

热门文章

  1. 查看html代码来下载mp4视频的一次记录
  2. 液晶显示屏简介以及与CRT显示屏的对比
  3. SQL SERVER2000企业版安装过程-如何选择授权模式?
  4. 浅论汽车电子行业的汽车开放系统架构AUTOSAR
  5. python求这个三位数的数字之和及乘积(超详细解答)
  6. C语言中u8 u16 u32含义,有关stm32的问题,程序里面的u8、u16这些是什么意思啊
  7. STM8S的按键PWM调光灯历程
  8. 使用同花顺获取单只股票的所有历史日线数据
  9. 路由器与计算机的ip地址,路由器ip地址,教您怎么样查看路由器的IP地址
  10. 威联通NAS网络存储器快速安装指南——从零搭建一个文件存储平台