In the database, I add time in the time () format, i.e. in seconds. When the operation time is returned to the client, he sees the server time, not his own. How to convert server time correctly?

As an idea, it is to find out the time in seconds on the device, with a request to send this time to the server. There calculate the difference.

Tell me if anyone has a ready-made algorithm and code.

  • new Date(timestampFromPhp*1000) - vp_arth
  • timestampFromPhp - what is it? - Vitaliy Fesyura
  • this is the result of calling the time() function on the server (in php ) - vp_arth

1 answer 1

The time() function that you use returns UNIX-timestamp, which in turn is notable for being independent of the time zone (seconds are GMT).

Transferring it to the client, you can instantiate the date object with its help, and the date will interpret the UNIX-timestamp correctly, taking into account the current time zone of the client.

The only time that needs to be taken into account is in the js timestamp given in milliseconds:

 //let ts = <?=time()?>; let ts = 1488575448; let now = new Date(ts * 1000); console.log(now.toTimeString()); // Разумеется, время можно форматировать самостоятельно console.log( [ now.getHours(), now.getMinutes(), now.getSeconds()] .map(d => d < 10 ? '0'+d : d) .join(':'));