2017-04-20 62 views
0

我的代码如下:PHP EDT/EST时间转换

<?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功能时。我怎样才能做到这一点?

回答

0

根据文档也没有办法关闭DST,它不应该做的

不过,我认为这样的事情可能工作:

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使用'美国/太平洋'应该使时间EDT。我还将DST更改为bool,使其更好读

+0

谢谢至少您已回答 –