首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在php中显示股票市场的剩余时间

问如何在php中显示股票市场的剩余时间
EN

Stack Overflow用户
提问于 2016-09-28 16:54:00
回答 1查看 157关注 0票数 0
代码语言:javascript
复制
<?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正在显示我的系统当前时间而不是纽约时间,所以它通过考虑我的系统时间来显示剩余时间。我想显示美国当前时间,这样我可以很容易地显示剩余时间。提前感谢。

EN

回答 1

Stack Overflow用户

发布于 2016-09-28 17:31:34

我认为这更接近于您的需求。

我已经确保在两个DateTime对象上都设置了特定的时区,这样就不会产生混淆

这也考虑到了市场可能已经开放的可能性,并告诉你还有多长时间才能关闭。

它在所有情况下都使用Todays,所以应该处理任何夏令时。

还有一个用于当前时间的时间设置器,因此您可以查看在一天中的不同时间发生了什么,有关设置特定时间的信息,请参阅$now->setTime(10, 30, 0);。

代码语言:javascript
复制
<?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;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39742511

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档