首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >php /EST时间转换

php /EST时间转换
EN

Stack Overflow用户
提问于 2017-04-20 08:01:15
回答 1查看 1.1K关注 0票数 0

我的代码如下:

代码语言:javascript
复制
<?php

function my_time($zone,$dst){
  if ($dst=='on') {
    // bellow codes will return time date when DST is on/EDT time
    $dateTime = new DateTime('now', new DateTimeZone($zone));
    $dst_on = $dateTime->format("d-m-Y h:i A");
    return $dst_on;
  }elseif ($dst=='off') {
    // bellow codes will return time date when DST is off/EST time
    $dateTime = new DateTime('now', new DateTimeZone($zone));
    $dst_off = $dateTime->format("d-m-Y h:i A");
    return $dst_off.' (Wrong output, i need DST off/EST output here! Please help)'; // Please help me to return dst off / EST time here
  }
}

echo my_time('America/New_York','off');

?>

当将off参数传递给my_time函数时,我需要正确的输出。我怎样才能做到这一点?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-04-20 09:11:26

根据,没有办法关闭DST,也不应该做

然而,我认为这样的事情可能会奏效:

代码语言:javascript
复制
function my_time($zone, $dst = true) {

    $dateTime = new \DateTime('now', new \DateTimeZone($zone));

    if ($dst) {
        $dateTime->setTimezone(new \DateTimeZone('US/Pacific'));
    }

    return $dateTime->format('d-m-Y, h:i A');
}

echo my_time('America/New_York', false);

我稍微修改了一下您的代码,但是从我在this上看到的使用'US/Pacific‘的情况来看,应该会有时间使用EDT。我还把DST改成了bool,这样读起来就更好了。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43513614

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档