Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is there a difference between JavaScript and PHP timestamp

I have created a JavaScript timestamps and also a PHP timestamp. There is about 170 sec difference between them.

  • 1302162686 PHP - time()
  • 1302162517 JavaScript - Math.round(new Date().getTime() / 1000)

Can anyone please tell me why I'm having this issue?

like image 714
Omer Abbas Avatar asked Apr 07 '11 07:04

Omer Abbas


People also ask

What is use of timestamp in PHP?

Returns the current time measured in the number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT). Note: Unix timestamps do not contain any information with regards to any local timezone.

What is a JavaScript timestamp?

Getting the Current Time Stamp const currentDate = new Date(); const timestamp = currentDate. getTime(); In JavaScript, a time stamp is the number of milliseconds that have passed since January 1, 1970.

How to timestamp in PHP?

The task can be done by using the strtotime() function in PHP. It is used to convert English textual date-time description to a UNIX timestamp.

What JavaScript data type is used to store a timestamp?

The Date object is a built-in object in JavaScript that stores the date and time. It provides a number of built-in methods for formatting and managing that data.


1 Answers

PHP is executed on the server side, and in your example, JavaScript works on the client-side.

Both sides have their own time configuration. For the server, time zone settings etc. will stay the same (unless you change them), but the server has no idea which time zone the current visitor is in. There’s no way for you to control that.

If I change my system clock on my laptop, it will influence client-side JavaScript date/time, but your server timer won’t be affected.

like image 143
Mathias Bynens Avatar answered Sep 23 '22 03:09

Mathias Bynens