Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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(); ?> 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?

P.S: is it useful for calculating the time interval? or it is used for storing the current time stamp?

like image 599
Ibrahim Azhar Armar Avatar asked Aug 13 '10 10:08

Ibrahim Azhar Armar


1 Answers

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.
like image 92
theomega Avatar answered Sep 29 '22 08:09

theomega