【问题标题】:how do i convert timezone string into standard timezone format using php如何使用 php 将时区字符串转换为标准时区格式
【发布时间】:2012-11-05 12:54:55
【问题描述】:

大家好,

我有一个时区字符串,例如:

Mon Nov 05 2012 06:54:06 GMT-0600 (Central America Standard Time)

需要使用 php 将这些字符串转换为如下 标准时区格式

(GMT-06:00)-Central America

代码

echo $tz = "Mon Nov 05 2012 06:54:06 GMT-0600 (Central America Standard Time)";

echo $tz2 = substr($tz, 0, 34);

echo date("P", strtotime("$tz2"));

结果

Mon Nov 05 2012 06:52:37 GMT-0600 (Central Standard Time)

Mon Nov 05 2012 06:52:37 GMT-0600

+00:00

试用参考:Date

但不会产生这种格式。现在需要帮助的伙伴,谢谢...

【问题讨论】:

  • 您是否可以控制时区的格式?

标签: php string date time timezone


【解决方案1】:

新编辑 - 试试这个...

$tz = "Mon Nov 05 2012 06:54:06 GMT-0600 (Central America Standard Time)";

$time = explode(" ",$tz);
$timezone = explode("(", str_replace(")","",$tz)); // would be better to use a regex
$new_time = "(".$time[5].")-".$timezone[1];

echo $new_time;  // gives (GMT-0600)-Central America Standard Time

退出前的旧代码.....

$tz = "Mon Nov 05 2012 06:54:06 GMT-0600 (Central America Standard Time)";
$new_time = strtotime($tz);

$new_date = date( '(P)-e', $new_time );
echo $new_date;

(-05:00)-美国/纽约

【讨论】:

  • 它显示如下:(+00:00)-UTC,因此时区不适用于localhost
  • 查看上面的代码,我已经更新了。仅当日期信息字符串的布局相同时才有效
【解决方案2】:

echo date("P", strtotime("$tz2"));

回声

GMT-0600

恐怕地区的名称不可用。

【讨论】:

  • 我在 localhost 上试过,它显示:+00:00,可能在localhost 上不起作用
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-23
相关资源
最近更新 更多