Home  >  Article  >  Backend Development  >  Comparison of timestamps in php and timestamps in javascript_PHP Tutorial

Comparison of timestamps in php and timestamps in javascript_PHP Tutorial

WBOY
WBOYOriginal
2016-07-14 10:10:00936browse

Comparison of timestamps in php and timestamps in javascript. Essentially, they are the same thing. However, if they are compared for equality, they are still a little different. If you are not careful, you will make a mistake. Astray, so here are two differences that are easy to overlook for your reference:

1) Unit problem: When fetching timestamp in PHP, it is mostly obtained through the time() method. The value it obtains is in seconds, while the value obtained in JavaScript is obtained from the getTime() method of the Date object. The unit is milliseconds, so to compare whether the times they obtain are on the same day, you must pay attention to converting their units to the same, 1 second = 1000 milliseconds, and you understand the rest, haha.

2) Time zone issue: As mentioned in the first point, the time() method is used in PHP to obtain the timestamp. For the convenience of display, we will set the time zone of the current server in the PHP code, such as mainland China The server is usually set to the East Eighth District, so that the time() method obtains the method no longer from 0:00:00 on January 1, 1970, but from 8:00 on January 1, 1970 It starts from 0 minutes and 0 seconds, and there are usually no time zone related settings in js, so the starting point for calculation is 0 hours, 0 minutes and 0 seconds on January 1, 1970, so it is easy to cause inconsistencies in this place.

Materialism tells us that we must see the essence of things through the phenomenon of things. Two timestamps are essentially the combination of year, month, day, hour, minute, and second. If the result does not match the expected result, we must not The best way is to output their year, month, day and other values, and compare them one by one, and you can easily find the problem.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/477558.htmlTechArticleComparison of timestamps in php and timestamps in javascript. Essentially, they are the same thing. But if the two are to be compared equally, they are still a little different. If you don’t pay attention...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn