我的代码如下:
<?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函数时,我需要正确的输出。我怎样才能做到这一点?
发布于 2017-04-20 09:11:26
根据,没有办法关闭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上看到的使用'US/Pacific‘的情况来看,应该会有时间使用EDT。我还把DST改成了bool,这样读起来就更好了。
https://stackoverflow.com/questions/43513614
复制相似问题