29

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 Answers 12

18

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);
}
Sign up to request clarification or add additional context in comments.

2 Comments

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 ... :)
Please change to microtime(true)
16

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

4 Comments

Won't time()*1000 return zeros as the last 3 digits?
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
Since PHP 5.0.0 microtime(true) returns time with milliseconds as a double value.
time()*1000 is an awful answer :-(
10

Use this:

intval(microtime(true)*1000)

Comments

5

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

Comments

4

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

Comments

2

Simple answer for PHP:

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

Comments

2

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

Comments

1

here's an improved version of your function, ensuring compatibility with PHP 7 while maintaining functionality for PHP 5:

function timestamp_ms(): int {
    $times = gettimeofday();
    $seconds = sprintf('%d', $times["sec"]);  // Use sprintf for integer formatting
    $milliseconds = sprintf('%03d', floor($times["usec"]/1000));  // Use sprintf for zero-padding

    return intval($seconds . $milliseconds);
}

Comments

0

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

Comments

0

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.

Comments

-1

Something like this:

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

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

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.