2017-04-20 96 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

謝謝至少您已回答 –