27
votes

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 obviously 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 as well. The microtime() function 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.

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

12

12 Answers

16
votes

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á.

17
votes

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

Use this:

intval(microtime(true)*1000)
5
votes

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;
    }
}
4
votes

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

2
votes

Simple answer for PHP:

function exact_time() {
    $t = explode(' ',microtime());
    return ($t[0] + $t[1]);
}
2
votes

To get millisecond timestamp from PHP DateTime object:

<?php
date_default_timezone_set('UTC');
$d = new \DateTime('some_data_string');
$mts = $d->getTimestamp().substr($d->format('u'),0,3); // millisecond timestamp
1
votes

PHP 7 This function has its return type declared.

function timestamp_ms(): int {
    $times = gettimeofday();
    $seconds = strval($times["sec"]);
    $milliseconds = strval(floor($times["usec"]/1000));
    $missingleadingzeros = 3-strlen($milliseconds);
    if($missingleadingzeros >0){
        for($i = 0; $i < $missingleadingzeros; $i++){
            $milliseconds = '0'.$milliseconds;
        }
    }
    return intval($seconds.$milliseconds);
}

PHP 5

function timestamp_ms() {
    $times = gettimeofday();
    $seconds = strval($times["sec"]);
    $milliseconds = strval(floor($times["usec"]/1000));
    $missingleadingzeros = 3-strlen($milliseconds);
    if($missingleadingzeros >0){
        for($i = 0; $i < $missingleadingzeros; $i++){
            $milliseconds = '0'.$milliseconds;
        }
    }
    return intval($seconds.$milliseconds);
}
0
votes

when you need the millisecond in str format, I think you should use:

public function get_millisecond() {
    list($milliPart, $secondPart) = explode(' ', microtime());
    $milliPart = substr($milliPart, 2, 3);
    return $secondPart . $milliPart;
}

this will fix the bug int some get millisecond example where the milli part is like : 0.056. some example convert the milli part to float, your will get 56 instead of 056. I think some one want 056.

especially when you need the millisecond to order some data.

hope will help. :)

0
votes

I recently had this problem to get a timestamp in milliseconds. To just multiply the unix timestamp by 1000 did not resolve the problem because i had to compare two database entrys very precicely. Aparently the php datetime object can´t handle milliseconds/microseconds but its stored in the datetime string anyway. So here is my solution:

$dateObject = new \DateTime('2015-05-05 12:45:15.444', new \DateTimeZone('Europe/London')); $millis = $dateObject->format('v'); echo $dateObject->getTimestamp()*1000+$millis;

This should also work with microseconds if you use format->('u') (and of course multiply the timestamp by 1000000) instead. I hope you find this useful.

-1
votes

Something like this:

$mili_sec_time = $_SERVER['REQUEST_TIME_FLOAT'] * 1000;

Gives float type representing miliseconds from UNIX epoch to starts of the request.

-1
votes
$timestamp = str_replace(".","",number_format((float)microtime(true),2,'.',''));