Time Sync() PHP and Javascript

Asked

Viewed 941 times

6

if I generate a time() on the PC purchased in Brazil it will result a team according to our time and such, if I repeat the process in China, USA or any other place with a local PC or time() will be different. My doubts are:

How to define that the team() results the US team() for example?

How to define my tipo time() by javascript for it to send to PHP and adapt the team()?

Type I’m accessing a server where the time() refers to the time Brazil, but my team() refers to the US and I want to warn the server (through Javascript) my type of team to synchronize with my time.. you understand that?

  • http://stackoverflow.com/questions/11820718/convert-utc-offset-to-timezone-or-date

4 answers

5

It looks like you’re getting trouble where you don’t have it. According to the documentation, the function time of PHP:

Returns the current time measured in the number of seconds since the Unix Era (January 1 1970 00:00:00 GMT).

And for the Javascript, Date.prototype.getTime() works the same (but in milliseconds instead of seconds):

The value returned by the getTime() method is the number of milliseconds Since 1 January 1970 00:00:00 UTC.

The two functions already work in sync. Therefore:

// Em JS:
var agora = new Date().getTime();
// Em PHP:
$agora = time() * 1000;
// Ou, com mais precisão:
// $agora = microtime(true) * 1000;
  • Time varies by local time, both client and server time.

  • @Ismaelmiguel It doesn’t vary, not in this format.

  • What is the result of running new Date(1429760063270)? Here the date and time is the comment, with about 2m difference.

  • Ismael, your server or client must be late.

  • Bfavaretto, did not know that the team was GMT 0:00 always.

  • @Ismaelmiguel When printing the date on the console, JS formats the value, and only then takes into account Timezone. But the number of milliseconds since 1/1/1970 0:00:00 UTC is independent of this. The time since that moment is the same in any Timezone, because the moment is fixed, in a predefined Timezone.

  • I apologize then. But in PHP, a timestamp of -3600 will give this date in the Azores, but not in mainland Portugal. However, a timestamp of 3600 will be that date in mainland Portugal, but will be difference in Spain.

  • 1

    timestamp represents the date in time zone 0:00, but the other functions that will do the printing/formatting will consider the local time zone.

  • @Ismaelmiguel That’s what Édipo said, the timestamp has a fixed zone, while the formatted dates can take into account the local configuration of the server or client. The suggestion of my reply is to use timestamps only for comparison/synchronization purposes.

  • @Édipocostarebouças I apologize for my misinterpretation.

  • The answer is correct, but if the team is being generated wrong just set the time zone for which you need, In linux vc can change this using this command "dpkg-reconfigure tzdata"

Show 6 more comments

3

To return which is the current Timezone, you can use the getTimezoneOffset.

var timezoneoffset = new Date().getTimezoneOffset();

This will return the difference in minutes of local time and UTC time (0:00).

These minutes also consider daylight saving time.

Instead of using this, I suggest you use UTC in the backend and frontend,

Javascript is supported to take your local date and time and convert to Timezone UTC, for example:

For example,

var timestamp = new Date().getTime();

Will return a timestamp based on time zone UTC (0:00)

var d = new Date(1429016291946);

or

var d = new Date();
d.setTime(1429016291946);

It will mount a date from the timestamp. I put the two forms because I did not find documentation on the first, although it works.

In PHP, the function time returns a timestamp in time zone 0:00.

Javascript works with timestamp in milliseconds and PHP with seconds, at some point in your code you will have to truncate the value or multiply by 1000, normalizing for javascript or PHP.

References

  • Or else use gmdate('T'), which also returns a timestamp based on GMT +00:00.

  • Now, if you’re printing dates with PHP, you really have to figure out the user’s Timezone.

  • Comments are missing. But run there new Date(1429788190538) and see the result. Almost certainly it will not be "Thu Apr 23 2015 12:23:00 GMT+0100 (Hora de Verão de GMT)".

  • Thu Apr 23 2015 08:23:10 GMT-0300 (E. South America Standard Time), on the console, printout according to my local time.

  • Try to use it that way, var d = new Date(); d.setTime(1429788190538); I didn’t find much documentation talking about passing the timestamp directly in the constructor.

  • Therefore, the interpretation of timestamp varies. What O.P. wants is that nay range. And the documentation is here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date and here: http://www.w3schools.com/js/js_dates.asp

  • The interpretation in javascript is for you to print the date in a localized way, this behavior is correct.

  • So the answer is simple: var time=new Date(<?=gmdate('t')?>) or var time=new Date(<?=gmmktime()?>). To have time in GMT. UTC is based on GMT. The difference is that UTC already has time change.

  • then of your answer.

Show 4 more comments

2

I believe the function you seek is date_default_timezone_set

Example of use:

date_default_timezone_set('America/Sao_Paulo');

At this link, you find the supported Timezones.

Read more about, in the documentation.

Hug

  • not to mention the part of Javascript

  • 1

    explain your code better, it is a little vague what you want to do with javascript.

1

Not much code required.

In fact, with a line you can solve the problem at once:

var time=new Date(<?=gmmktime()?>000) //Equivalente a <?php echo gmmktime();?>
var time=new Date(<?=gmdate('t')?>000) //Produz o mesmo resultado

The 000 is necessary because PHP returns time in seconds, while Javascript waits for time in milliseconds. Adding 000 at the end of the number (or multiply by 1000) solves the difference.

To get the time, based on server time, without having a setInterval();, some amendments are needed:

var time=new Date(<?=gmmktime()?>000); //hora do servidor
var time_dif=(new Date())-time; //subtrai a diferença em milisegundos em relação às duas datas

/* [... algum tempo depois ...] */

var server_time=new Date(); //cria uma nova data
server_time.setTime(server_time-time_diff); //subtrai a diferença

It doesn’t take much more than this.

  • Are you forgetting the accuracy difference remembered in all other answers.

  • Must be a few milliseconds. But if accuracy is so important, the O.P. should arrange a quartz clock connected to the computer and access the time at a constant time. Then that time had to be sent over the internet with a constant latency and subtraction had to be done. Other than that, this comes.

  • You tested your code?

  • What you’re doing is cutting the last 3 numbers out of a timestamp, if you do this with PHP do you think it will work? Because you are doing this with Javascript. After printing the timestamp, add 000 , example <?=time()?>000 that yes it will work.

  • @I forgot that PHP returns time in seconds. It’s already fixed.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.