3

I need to show a timer central to PHP SERVER DATE, here which is date1. (Its about a meeting which was set in Belgium time and others from different country joins the meeting to see duration of the meeting realtime.)

date1 variable value coming from PHP server located in Belgium. But the users are in USA, Middel east, Asia, Australia, and there Browser date is not same as PHP server date.

How can i get the date2 value without crawling to server as Belgium date/time? so that i can show the gape realtime on date2.

var date1 = new Date("10-01-2016 09:00:00"); // Meeting at PHP SERVER date 
var date2 = new Date("10-01-2016 09:30:32"); // User joined remotely but Browser date is wrong here

var diff = date2.getTime() - date1.getTime();
var msec = diff;
var hh = Math.floor(msec / 1000 / 60 / 60);
msec -= hh * 1000 * 60 * 60;
var mm = Math.floor(msec / 1000 / 60);
msec -= mm * 1000 * 60;
var ss = Math.floor(msec / 1000);
msec -= ss * 1000;

alert(hh + ":" + mm + ":" + ss);
gagan mahatma
  • 336
  • 2
  • 9
  • Normally it's easiest to have all DB/PHP/Server side servers use the same timezone, like UTC. Then if you need localized times/dates you do that for each user . – JimL Jan 10 '16 at 12:14
  • On Server Side its set to Belgium time zone only. So that value of date1 is taken from the Server when user first logs in. But the problem is how can i now tell date2 that he has to show the time of Belgium (not crawling from the server but locally) –  Jan 10 '16 at 12:15
  • 1
    Do you want all users to see how long the meeting has been going on, or how long it still is before the meeting starts / stops, without actually showing an absolute time? Is that the question? Would be nice if you could give a more detailed example, and what the output should be in the browser. – trincot Jan 10 '16 at 12:19
  • YES - i need to show USA, Asia, Middle east users (all) how long the meeting has been going on. So everyone know the meeting was at 9AM but its going on for 2 hour 00:02:03 like so. –  Jan 10 '16 at 12:22

1 Answers1

1

I think you could use this answer: Getting the client's timezone in JavaScript to get the client (browser) timezone offset, the php server could sent this information along with date1, and from here it's easy to convert as you already did.

Another approach could be that the server also sends the current time (similar to what the Date http header could be) and the client could save in a variable the time on the client at the moment it got the http, then calculate the difference between the server clock and client clock. This way it could not only calcualte that 9AM in Belgium is 2AM in New York, but also display a counter on the client that would be synchronized to the server time (meaning even if the client's clock is off by 2 minutes, the counter could be accurate to the server's clock, and so all the people would see the same counter, no matter of their client clock's inaccuracy (which is a nice feature even if we don't think of different timezones))

So in your php besides date1="10-01-2016 09:00:00" you would also send

server_date="<?php date('...') ?>"

Use http://php.net/manual/en/function.date.php for the format string :)

Then in javascript at the very beginning of the onLoad callback you would do:

client_date = new Date();

Now you can do your arithmetic (in pseudo code):

elapsed_since_start = date1 + (server_date - client_date)

Of course if you need to take into account the different timezones then you'll need to account for that too:

elapsed_since_start = (date1-server_tz_offset) + ((server_date-server_tz_offset) - (client_date-client_tz_offset))
Community
  • 1
  • 1
Gavriel
  • 18,880
  • 12
  • 68
  • 105
  • Second method wont work. Because some users will join the meeting later because of traffic jam or etc etc. We need to show the if he is late that is he 10 minute late. Meeting is already running. –  Jan 10 '16 at 12:39
  • It will work, why not? Let's say I'm "actually" 10 minutes late because of the traffic, and my laptop's clock is 3 minutes late compared to the server's clock. The question is whether you want to see I'm 10 or 13 minutes late. – Gavriel Jan 10 '16 at 12:42
  • I want to see 10 minutes late (actual late based on server time stamp). –  Jan 10 '16 at 12:54
  • 1
    Ok, so that's why you'll need my 2nd approach to calculate the difference between the server and the client clock. If you would only use the 1st aproach, then it would display 7 minutes, because it would think the time is 09:07, although the "real" ot server time is already 09:10. – Gavriel Jan 10 '16 at 13:00
  • Possible to put an example please? cause its confusing your last statement is exactly what i need to implement. –  Jan 10 '16 at 13:05
  • I edited my answer, I hope that helps. I see that doing the arithmetics should not be a problem to you. – Gavriel Jan 10 '16 at 13:12