阿姆斯特丹是来自任何国家的游客的时间

时间:2014-05-06 18:23:08

标签: javascript php ajax date time

我有时间剧本,我需要这样的结果,但不是当地时间我想向阿姆斯特丹展示eveyone的时间,有人可以帮助我吗?我的代码是:

  <!DOCTYPE html>
  <html>
  <head>
    <script>
       function startTime() {
         var today=new Date();
         var h=today.getHours();
         var m=today.getMinutes();
         var s=today.getSeconds();
         m = checkTime(m);
         s = checkTime(s);
         document.getElementById('txt').innerHTML = h+":"+m+":"+s;
        var t = setTimeout(function(){startTime()},500);
      }

      function checkTime(i) {
        if (i<10) {i = "0" + i};  // add zero in front of numbers < 10
           return i;
       }

          setInterval(function(){ startTime() }, 1000);
     </script>
  </head>


  <body>

    <div id="txt"></div>

  </body>
 </html>

http://www.w3schools.com/js/tryit.asp?filename=tryjs_timing_clock

我想要一样,但只会显示来自任何国家的每位访客的阿姆斯特丹时间。提前谢谢你:)

3 个答案:

答案 0 :(得分:0)

试试这个:

function worldClock(zone, region){
var dst = 0
var time = new Date()
var gmtMS = time.getTime() + (time.getTimezoneOffset() * 60000)
var gmtTime = new Date(gmtMS)
var day = gmtTime.getDate()
var month = gmtTime.getMonth()
var year = gmtTime.getYear()
if(year < 1000){
year += 1900
}
var monthArray = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December")

var monthDays = new Array("31", "28", "31", "30", "31", "30", "31", "31", "30", "31", "30", "31")
if (year%4 == 0){
monthDays = new Array("31", "29", "31", "30", "31", "30", "31", "31", "30", "31", "30", "31")
}
if(year%100 == 0 && year%400 != 0){
monthDays = new Array("31", "28", "31", "30", "31", "30", "31", "31", "30", "31", "30", "31")
}

var hr = gmtTime.getHours() + zone
var min = gmtTime.getMinutes()
var sec = gmtTime.getSeconds()

if (hr >= 24){
hr = hr-24
day -= -1
}
if (hr < 0){
hr -= -24
day -= 1
}
if (hr < 10){
hr = " " + hr
}
if (min < 10){
min = "0" + min
}
if (sec < 10){
sec = "0" + sec
}
if (day <= 0){
if (month == 0){
    month = 11
    year -= 1
    }
    else{
    month -= 1
    }
day = monthDays[month]
}
if(day > monthDays[month]){
    day = 1
    if(month == 11){
    month = 0
    year -= -1
    }
    else{
    month -= -1
    }
}

    if (region == "Europe"){
    var startDST = new Date()
    var endDST = new Date()
    startDST.setMonth(2)
    startDST.setHours(1)
    startDST.setDate(31)
    var dayDST = startDST.getDay()
    startDST.setDate(31-dayDST)
    endDST.setMonth(9)
    endDST.setHours(0)
    endDST.setDate(31)
    dayDST = endDST.getDay()
    endDST.setDate(31-dayDST)
    var currentTime = new Date()
    currentTime.setMonth(month)
    currentTime.setYear(year)
    currentTime.setDate(day)
    currentTime.setHours(hr)
    if(currentTime >= startDST && currentTime < endDST){
        dst = 1
        }
}
    if (dst == 1){
    hr -= -1
    if (hr >= 24){
    hr = hr-24
    day -= -1
    }
    if (hr < 10){
    hr = " " + hr
    }
    if(day > monthDays[month]){
    day = 1
    if(month == 11){
    month = 0
    year -= -1
    }
    else{
    month -= -1
    }
    }
return monthArray[month] + " " + day + ", " + year  + " " + hr + ":" + min + ":" + sec + " DST"
}
else{
return monthArray[month] + " " + day + ", " + year  + " " + hr + ":" + min + ":" + sec
}
}   

var amstime = worldClock(1, "Europe") ;
alert("amsterdam time is: " + amstime);

LIVE DEMO

SOURCE 查看此源代码,了解如何每秒更新一次。

答案 1 :(得分:0)

为什么不使用它:<?php date_default_timezone_set('Europe/Amsterdam'); ?>所以现在<?php date('d-m-Y') ?>将始终是阿姆斯特丹时间

答案 2 :(得分:0)

查看Moment.js

您可以使用momentjs.com/timezone/data/上的数据构建器构建仅包含所需时区的数据集。

<script>
    moment().tz("Europe/Amsterdam").format();
</script>