0

I want to show up the time over my website based over the location of the user, let’s say if user one browsing the website is from USA than the time should be what is in USA currently and same for China etc. and all.

I was wondering if there exists a JavaScript plugin for it but I didn’t find any as dynamic as I want, my requirements include:

  • Something that can be fully stylized according to website theme (no iframes)
  • The pattern I want is to be in (HH:MM:SS)
  • It should be asynchronous like the second [SS] keep ticking and the time keep updating

Is this possible, a way around to achieve it?

Maven
  • 14,587
  • 42
  • 113
  • 174

2 Answers2

1

Wouldn't this be enough?

html

<span id="time"></span>

js

$(function() {
    var time = $("#time");

    function getTime() {
        var now = new Date(),
            hours = now.getHours(),
            minutes = now.getMinutes(),
            seconds = now.getSeconds();

        return (hours < 10 ? "0" + hours : hours) + ":" + (minutes < 10 ? "0" + minutes : minutes) + ":" + (seconds < 10 ? "0" + seconds : seconds);
    }

    setInterval(function() {
        time.text(getTime());        
    }, 1000);
});​

Example

Andreas
  • 21,535
  • 7
  • 47
  • 56
  • This is not working, plus I don’t wanna rely on client machine for the current and ‘correct’ time – Maven Dec 12 '12 at 12:22
  • The example still works for me. And why you think your server will do a better job here? Who knows the local time better than the local client itself? – Andreas Dec 12 '12 at 12:45
0

Just putting out the local system time of the user isn't enough?

If you want a server based solution, please take a look at the solution here. You have to find out the users timezone first and then manipulate the server time with an offset from the timezone using $dt->setTimezone(new DateTimeZone("Europe/Berlin"));.

Community
  • 1
  • 1
David Müller
  • 5,291
  • 2
  • 29
  • 33