php EDT / EST时间转换

时间:2017-04-20 08:01:15

标签: php

我的代码如下:

<?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函数时,我希望输出正确。我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:0)

根据文档,无法关闭夏令时,不应该这样做

但是,我觉得这样的事情可能有用:

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,这使得阅读更好一点