将时间戳转换为多个时区 [英] converting a timestamp into multiple timezones

查看:567
本文介绍了将时间戳转换为多个时区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

php中的时区转换


这是一个简单的例子,



我有一个包含时间戳的变量 $ time



现在,我想把它转换成不同时区的时间戳记



有没有办法可以不使用这个功能呢?code > date_default_timezone_set()多次?



我知道我的观点可能会令人困惑,点数少,请澄清评论中的任何疑问。 >

如果我有100个电话来更改它: -


  1. 它影响我的其他功能

  2. 函数名称建议其默认值

  3. 减慢脚本。


解决方案

您可以使用 DateTime 对象,并根据需要使用 SetTimezone()方法。



例如,以下代码: -

  $ datetime = new DateTime(); 
var_dump($ datetime-> getTimezone() - > getName());
var_dump($ datetime-> format(DateTime :: ISO8601));
$ datetime-> setTimezone(new DateTimeZone('UTC'));
var_dump($ datetime-> format(DateTime :: ISO8601));
$ datetime-> setTimezone(new DateTimeZone('America / New_York'));
var_dump($ datetime-> format(DateTime :: ISO8601));

给我以下输出: -

  string'Europe / London'(length = 13)
string'2012-06-20T20:33:29 + 0100'(length = 24)
string' 2012-06-20T19:33:29 + 0000'(length = 24)
string'2012-06-20T15:33:29-0400'(length = 24)

我的开发者服务器默认TZ是欧洲/伦敦,所以你可能会得到稍微不同的结果。



支持的时区这里的列表。


Possible Duplicate:
Timezone conversion in php

Here is a simple example of what i want to do.

I have a variable $time containing a timestamp.

Now, I want to convert it into timestamp of different timezones

Is there a way to do this without using the function date_default_timezone_set() multiple times ?

I know my point might seem confusing and point less so please clarify any doubt in comments.

If I have, for example, 100 calls to change it:-

  1. It affects my other functions
  2. The functions name suggests its default
  3. It slows down the script.

解决方案

You can use the DateTime object and set the time zone as required using the SetTimezone() method.

For example, the following code:-

$datetime = new DateTime();
var_dump($datetime->getTimezone()->getName());
var_dump($datetime->format(DateTime::ISO8601));
$datetime->setTimezone(new DateTimeZone('UTC'));
var_dump($datetime->format(DateTime::ISO8601));
$datetime->setTimezone(new DateTimeZone('America/New_York'));
var_dump($datetime->format(DateTime::ISO8601));

Gave me the following output:-

string 'Europe/London' (length=13)
string '2012-06-20T20:33:29+0100' (length=24)
string '2012-06-20T19:33:29+0000' (length=24)
string '2012-06-20T15:33:29-0400' (length=24)

My dev server default TZ is Europe/London, so you may get slightly different results.

There is a list of supported timezones here.

这篇关于将时间戳转换为多个时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆