admin管理员组文章数量:1202341
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?
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?
Share Improve this question edited Aug 3, 2015 at 18:37 Salman Arshad 272k84 gold badges442 silver badges533 bronze badges asked Apr 7, 2011 at 7:56 Omer AbbasOmer Abbas 751 silver badge5 bronze badges 1- 1 Do the server and the client have the same time set? – Felix Kling Commented Apr 7, 2011 at 7:57
6 Answers
Reset to default 14PHP 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.
PHP and JavaScript, both look at the system time. Whose system? The one they are running on. The server could be located in another country with different time, hence the difference.
Also, the client's (or less often, server's) clock could be incorrect.
One way, which I often use to counter this problem is like this:
var referenceTime = new Date('<?php echo date("M n, Y"); ?>');
// referenceTime is now the same as server time
PHP looks at the system time, which is the server running it.
JavaScript looks at the client's system, which could be any time.
php uses the time on your server, javascript will use the time on the client (users) machine.
Mathias is correct. Generally this should not happen with that big a difference because modern computers recognize their clocks drift over time and employ protocols such as NTP to keep their clocks in sync.
Nevertheless you should never assume the time at client and server is the same, for two reasons:
- Some clients/servers don't have clock adjustments (such as NTP) and their clocks drift away over time
- More importantly, many users/admins can be clueless or late in setting their time zone or adjusting daylight savings times, so the time given to you may be accurate to a second but be several hours off.
When comparing/calculating times, I would rely on the server only. You have no control over the client.
If you are concerned about consistency for whatever purpose, I recommend using the server as your time source and do timezone conversions if necessary:
This may be of interest: handling timezone conversion with php
本文标签: Why is there a difference between JavaScript and PHP timestampStack Overflow
版权声明:本文标题:Why is there a difference between JavaScript and PHP timestamp - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738646762a2104632.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论