简体   繁体   中英

why do we need to calculate time from year 1970?

In most of the Programming or scripting language such as PHP or JavaScript, i have come across the time function which tends to calculate the time from year 1970. for example in javascript if i use the function getTime() or in PHP the time() it returns an integer value.

In PHP.

the code with <?php echo time(); ?> <?php echo time(); ?> returns an integer value of 1281694425, what does the value returned by the PHP explains? how and when it is useful?

the same in javascript the getTime() returns the float value since 1970 as it says. by using the below script in JS

<script type="text/javascript">
var d=new Date();
document.write(d.getTime() + " milliseconds since 1970/01/01");
</script>

i want to know when is this type of functions useful to use? and how do we use it?

PS: is it useful for calculating the time interval? or it is used for storing the current time stamp?

It give you the seconds which passed by since the 1st January 1970. This is useful fore several reasons:

  1. Sorting of dates is as simple as sorting integers.
  2. Its easy to get the length of an interval, simple do t1-t2 and you get the difference in seconds again.
  3. No need to handle different date-formats (like MM/DD/YY or DD/YY/MM): Every programming language and every database supports integers.

如果您想了解此时间表示的历史记录,请查看: http//en.wikipedia.org/wiki/Unix_time

It means you if you recalculate the time from the integer in another time zone you will get the correct time for whichever timezone is accessing it. I believe :-)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM