Team Synchronization () PHP and JavaScript

6

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

How to set time () to result in US time () for example?

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

I am accessing a server where time () refers to the time in Brazil, but my team () refers to the US and I want to warn the server (via JavaScript), my team type so that it synchronizes With my time, do you understand?

    
asked by anonymous 14.04.2015 / 01:20

4 answers

5

It seems like you're getting trouble where you do not have it. According to the documentation, PHP's time function:

  

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

And for JavaScript, Date.prototype.getTime() works equal (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.

Both 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;
    
23.04.2015 / 03:12
3

To return what the current timezone is, you can use getTimezoneOffset .

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

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

These minutes are considered summer time as well.

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

JavaScript has support for picking up your local time and date and converting to UTC timezone, for example:

For example,

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

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

var d = new Date(1429016291946);

or

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

Will mount a date from the timestamp. I put both forms because I did not find documentation about the first one, although it works.

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

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

References

14.04.2015 / 01:59
2

I think the function you are looking for is date_default_timezone_set

Example usage:

date_default_timezone_set('America/Sao_Paulo');

In this link , you will find the Timezones supported.

Read more about this in the documentation.

Embrace

    
14.04.2015 / 01:29
1

You do not need a lot of code.

Incidentally, 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

000 is required because PHP returns the time in seconds, while Javascript waits for the time in milliseconds. Adding 000 to the end of the number (or multiplying by 1000) solves the difference.

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

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 does not take much more than this.

    
23.04.2015 / 13:59