显示并使用特定时区的时间

时间:2016-08-05 10:37:53

标签: javascript

所以我在澳大利亚有一个验光师的网站。我在网站上添加了一个小部件,显示当前时间,并告知商店当前是打开还是关闭。问题是,它将显示它是否仅为观众的时区打开/关闭,并且我想显示AEST(澳大利亚东部)的开/关。我不认为这是一个问题,但我们有来自西澳大利亚,印度,英国和美国的大量流量。

我已经搜索了几次并尝试了一些解决方案,比如添加默认时区,但代码没有用,我不确定是什么错。

这是代码



var serverTimestampMillis = <?php print time() * 1000 ?>;
var checkInterval = 1000;

var checkTime = function () {
    serverTimestampMillis += checkInterval;
    var now = new Date(serverTimestampMillis);
    //var now = new Date();

    var timeDiv = document.getElementById('timeDiv');
    var messageDiv = document.getElementById('messageDiv');
    timeDiv.innerHTML = now.toString();
    
    var dayOfWeek = now.getDay(); // 0 = Sunday, 1 = Monday, ... 6 = Saturday

    var hour = now.getHours(); // 0 = 12am, 1 = 1am, ... 18 = 6pm
    var minutes = now.getMinutes();
    // check if it is Mon - Thur 8:30am - 6:30pm
    if (dayOfWeek > 0 && dayOfWeek < 5) {
        if (hour == 8) {
            if (minutes >= 30) {
                messageDiv.innerHTML = 'We are currently open.';
                messageDiv.className= 'open'; 
            } else  {
                messageDiv.innerHTML = 'Sorry, we are closed.';
                messageDiv.className='closed';
            }
            
        } else if (hour > 8 && hour < 17) {
            messageDiv.innerHTML = 'We are currently open.';
            messageDiv.className= 'open';
        } else if (hour == 18) {
            if (minutes < 30) {
                messageDiv.innerHTML = 'We are currently open.';
                messageDiv.className= 'open';
            }
        } else {
            messageDiv.innerHTML = 'Sorry, we are closed.';
            messageDiv.className='closed';
        }
    }
    // check if it is a Friday 8:30am - 8pm
    else if (dayOfWeek == 5) {
        if (hour == 8) {
            if (minutes >= 30) {
                messageDiv.innerHTML = 'We are currently open.';
                messageDiv.className= 'open'; 
            } else  {
                messageDiv.innerHTML = 'Sorry, we are closed.';
                messageDiv.className='closed';
            }
        } else if (hour > 8 && hour < 20) {
            messageDiv.innerHTML = 'We are currently open.';
            messageDiv.className= 'open';
        }
    }
    // check if it is a Saturday 9am - 2pm
    else if (dayOfWeek == 6) {
        if (hour > 9 && hour < 14) {
            messageDiv.innerHTML = 'We are currently open.';
            messageDiv.className= 'open'; 
        }
    }
    // closed
    else {
        messageDiv.innerHTML = 'Sorry, we are closed.';
        messageDiv.className='closed';
    };  
}     
// check the time every 1000 milliseconds
setInterval(checkTime, checkInterval);
checkTime();
&#13;
&#13;
&#13;

任何帮助将不胜感激,谢谢!

2 个答案:

答案 0 :(得分:1)

如果你不在AEST,没有以编程方式获取当前的AEST时间,除非你在某处硬编码夏令时(假设你想要半年的ADST),但这不是非常健壮。

了解AEST在任何浏览器中的时间的唯一防弹方法是使用timeapi.org或this one等时间服务。

如果是我的网站,我会查看用户的时区,如果是悉尼,则显示聪明的“当前打开/关闭”消息,如果不是,则只显示静态营业时间。

答案 1 :(得分:0)

date_default_timezone_set('Australia/Sydney');

$current_date = date('d/m/Y H:i:s');