Getting unix timestamp in milliseconds in PHP5 and Actionscript3

Use this:

intval(microtime(true)*1000)

For actionscript3, new 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á.


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);
}

To normalize a timestamp as an integer with milliseconds between Javascript, Actionscript, and PHP

Javascript / Actionscript:

function getTimestamp(){
    var d = new Date();
    return Date.UTC(d.getFullYear(), d.getMonth(), d.getDate(), d.getHours(), d.getMinutes(), d.getSeconds(), d.getMilliseconds()).valueOf();
}

PHP:

function getTimestamp(){
    $seconds = microtime(true); // true = float, false = weirdo "0.2342 123456" format 
    return round( ($seconds * 1000) );
}

See PHP note at "ben at sixg dot com's" comment at: http://www.php.net/manual/en/function.gmmktime.php

EXCERPT: For most intents and purposes you can imagine that mktime() first converts your input parameters to GMT and then calls gmmktime() which produces a GMT timestamp.

So, time() always will return the same thing at the same actual moment, anywhere in the world.
gmmktime() and mktime(), when given specific time parameters, convert those time parameters FROM the appropriate timezone (GMT for gmmktime(), local time for mktime()), before computing the appropriate timestamp.


UPDATE:

On some versions of PHP, the timestamp with milliseconds is too large to display as a string. So use the sprintf function to get the string value:

PHP

function getTimestamp($asString=false){
    $seconds = microtime(true); // false = int, true = float
    $stamp = round($seconds * 1000);
    if($asString == true){
        return sprintf('%.0f', $stamp);
    } else {
        return $stamp;
    }
}