tags:

views:

179

answers:

3

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?

+6  A: 

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.
theomega
so basically you mean to say it is used to store the time difference or time interval and not the current time stamp?
Ibrahim Azhar Armar
Rather, a time stamp is an interval between a reference point and some other time. For example, the year 2010 is two thousand and ten years after a notable event. d.getTime() returns the number of milliseconds after 1970/01/01. 1281694425 *is* a time stamp.
Douglas
One thing to note - such timestamps represent an **instant in time**. This is easily confused when you have to do calculations at the date level. While it's seemingly easy to use such a timestamp to store a date (just set the clock to 00:00), it quickly gets awkward when time zones are introduced and/or some values participating in the calculations are not at 00:00. Thus if you need to store and work with dates, I'd recommend a different data type (or a LOT of caution).
Vilx-
+1  A: 

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 :-)

Dom
do you mean to say it is useful while calculating the time intervals? or can it be used for storing the current time stamp too?
Ibrahim Azhar Armar
Yes you can use it as a time stamp.
Dom
+5  A: 

If you want to know the history of this time representation, look at this: http://en.wikipedia.org/wiki/Unix_time.

Xupypr MV
that is quite useful, thank you.
Ibrahim Azhar Armar