views:

4704

answers:

3

In Actionscript, the Unix timestamp in milliseconds is obtainable like this:

public static function getTimeStamp():uint
     {
      var now:Date = new Date();
      return now.getTime();
     }

The doc clearly states the following:

getTime():Number Returns the number of milliseconds since midnight January 1, 1970, universal time, for a Date object.

When I trace it, it returns the following: 824655597

So, 824655597 / 1000 / 60 / 60 / 24 / 365 = 0.02 years. This is obviousely not correct, as it should be around 39 years. Question #1: What's wrong here?

Now, onto the PHP part: I'm trying to get the timestamp in milliseconds there aswell. microtime() returns either a string (0.29207800 1246365903) or a float (1246365134.01), depending on the given argument. Because I thought timestamps were easy, I was going to do this myself. But now that I have tried and noticed this float, and combine that with my problems in Actionscript I really have no clue. Question #2: how should I make it returns the amount of milliseconds in a unix timestamp?

Timestamps should be so easy, I'm probably missing something.. sorry about that. Thanks in advance.

EDIT: Answered the first question by myself. See below. EDIT2: Answered second question by myself aswell. See below. Can't accept answer within 48 hours.

+1  A: 

I used unsigned integer as the return type of the function. This should be Number.

public static function getTimeStamp():Number
     {
      var now:Date = new Date();
      return now.getTime();
     }

Think I got the function for getting milliseconds in PHP5 now.

function msTimeStamp() {
    return round(microtime(1) * 1000);
}
Tom
both right ... but do yourself and others a favour and change it to microtime(true) in PHP ... of course 1 works, but the parameter should be a boolean, simply because of semantics ... its really better to read ... for others, or for yourself in a couple of years ... :)
back2dos
Hmm so no upvote although I provided the correct answer earlier then you?
jitter
Sorry about that, accepted yours and upvoted.
Tom
A: 

microtime() in php5 returns unix timestamp with microseconds as per microtime() and if the get_as_float argument is not provided, it gives you a string formatted as "msec sec" so the first part is the millisecond part and the second is the second part. Just split it in two and you get the two parts of the timestamp

Knut Haugen
A: 

I can't tell you for actionscript3 as Date.getTime() should work.

In PHP you can simply call time() to get the time passed since January 1 1970 00:00:00 GMT in seconds. If you want milliseconds just do (time()*1000).

If you use microtime() multiply the second part with 1000 to get milliseconds. Multiply the first part with 1000 to get the milliseconds and round that. Then add the two numbers together. Voilá.

jitter
Won't time()*1000 return zeros as the last 3 digits?
Tom
Yes it will. With using time() you have the time exact to the second. If you really need the milliseconds real value too use microtime
jitter