<?php
date_default_timezone_set('America/New_York');
$current_time = time();
$now = new DateTime();
$b = $current_time;
$future_date = new DateTime('2011-05-11 09:30:00');
$interval = $future_date->diff($now);
echo $interval->format("%h hours, %i minutes, %s seconds");
?>我想要显示市场剩余时间,市场在上午9:30开盘,上面的代码运行良好,但问题是$current_time正在显示我的系统当前时间而不是纽约时间,所以它通过考虑我的系统时间来显示剩余时间。我想显示美国当前时间,这样我可以很容易地显示剩余时间。提前感谢。
发布于 2016-09-28 17:31:34
我认为这更接近于您的需求。
我已经确保在两个DateTime对象上都设置了特定的时区,这样就不会产生混淆
这也考虑到了市场可能已经开放的可能性,并告诉你还有多长时间才能关闭。
它在所有情况下都使用Todays,所以应该处理任何夏令时。
还有一个用于当前时间的时间设置器,因此您可以查看在一天中的不同时间发生了什么,有关设置特定时间的信息,请参阅$now->setTime(10, 30, 0);。
<?php
$now = new DateTime();
$now->setTimezone(new DateTimeZone('America/New_York'));
// To test what happens at different times of the current day
// Set a specific time for the NOW DateTIme
$now->setTime(10, 30, 0);
echo 'Now = ' . $now->format('d/m/Y H:i:s').PHP_EOL;
$opens = new DateTime();
$opens->setTimezone(new DateTimeZone('America/New_York'));
$opens->setTime(9, 30, 0);
echo 'Opens = ' . $opens->format('d/m/Y H:i:s').PHP_EOL;
$interval = $now->diff($opens);
// if invert == 1 its a minus difference so market is open
if ( $interval->invert == 1){
// Its already open
echo $interval->format("Market has been open for: %h hours, %i minutes, %s seconds").PHP_EOL;
// When will it close
$close = new DateTime();
$close->setTimezone(new DateTimeZone('America/New_York'));
$close->setTime(3, 30, 0);
$interval = $now->diff($close);
echo $interval->format("Market closes in: %h hours, %i minutes, %s seconds").PHP_EOL;
} else {
// it will open in
echo $interval->format("Market opens in: %h hours, %i minutes, %s seconds").PHP_EOL;
}https://stackoverflow.com/questions/39742511
复制相似问题