【问题标题】:Does PHP automatically convert standard time to day light savings time?PHP 会自动将标准时间转换为夏令时吗?
【发布时间】:2014-09-16 13:10:45
【问题描述】:

PHP 是否会自动将标准时间转换为夏令时?例如,我们需要PST[$toTimezone]timezone 的时间,但 PHP 正在返回 PDT。下面是我们正在使用的示例函数。

$fromTime = '2014-09-16 06:45:45';
$fromTimezone = 'GMT';
$toTimezone = 'PST';

function x($fromTime, $fromTimezone, $toTimezone,$format = 'l, F jS, Y g:i A T') {
  $fromTimezone = new \DateTimeZone($fromTimezone);
  $toTimezone = new \DateTimeZone($toTimezone);
  $orgTime = new \DateTime($fromTime, $fromTimezone);
  $toTime = new \DateTime($orgTime->format("c"));
  $toTime->setTimezone($toTimezone);
  return $toTime->format($format);
}

问候, Vamsi Krishna Grandhi

【问题讨论】:

  • 其实PHP是看服务器时间的。

标签: php date datetime time timezone


【解决方案1】:

PHP 会将时间转换为当前观察到的时区。因此,就纽约而言,今天将是东部夏令时间 (-4),而在一年的最后一天,纽约将是东部标准时间。不遵守夏令时的亚利桑那州凤凰城始终采用山区标准时间。

$date = new DateTime('2014-09-16', new DateTimeZone('America/New_York'));
echo $date->format('c T');

$date = new DateTime('2014-09-16', new DateTimeZone('America/Phoenix'));
echo $date->format('c T');

$date = new DateTime('2014-12-31', new DateTimeZone('America/New_York'));
echo $date->format('c T');

$date = new DateTime('2014-12-31', new DateTimeZone('America/Phoenix'));
echo $date->format('c T');

2014-09-16T21:12:03-04:00 EDT
2014-09-16T18:12:03-07:00 MST
2014-12-31T00:00:00-05:00 EST
2014-12-31T00:00:00-07:00 MST

【讨论】:

    猜你喜欢
    • 2021-01-16
    • 1970-01-01
    • 2020-05-22
    • 2015-11-29
    • 2020-02-04
    • 2017-09-10
    • 2019-04-08
    • 2013-11-09
    • 2016-06-14
    相关资源
    最近更新 更多