Get current timestamp with milliseconds
It can print less digits, because it is a float value. So if you get 1234.0005
, that `.0005 actually means 500 microseconds. The zeroes after the 5 are lost because it's still an unformatted float value.
The function microtime
is based on the system call gettimeofday()
, which also isn't accurate to the microsecond. Commonly it's accurate to 10 microseconds. See also Linux - Is gettimeofday() guaranteed to be of microsecond resolution.
-edit- I see the specs in your question have changed from microseconds to milliseconds.
To get the float value you have as an integer, you can multiply by a value. The float value represents seconds. Multiply by 1000 to get milliseconds or 1,000,000 to get microseconds. Since the specs (now) say milliseconds, you should multiply by 1000. 10k will give you accuracy of 1/10ms = 100μs. A millisecond is one thousandth of a second. A microsecond is one millionth of a seconds.
Long story short, to get the time in integer milliseconds, use this:
$milliseconds = intval(microtime(true) * 1000);
Note: there is a reason why you get the time as a string or a float by default. The reason is that on 32 bit systems, PHP's integer is also 32 bits and is not large enough to contain the timestamp including milliseconds and microseconds. So this solution will only work well on a 64 bit system.
Datetime to Timestamp in milliseconds in PHP
Pretty self explanatory code, so I wont say much.
date_default_timezone_set('Australia/Sydney'); // set timezone
$yourdate = '2016-03-22 14:30';
$stamp = strtotime($yourdate); // get unix timestamp
$time_in_ms = $stamp*1000;
If you want to display it properly.
echo number_format($time_in_ms, 0, '.', '');
Getting date format m-d-Y H:i:s.u from milliseconds
php.net says:
Microseconds (added in PHP 5.2.2). Note that
date()
will always generate000000
since it takes an integer parameter, whereasDateTime::format()
does support microseconds ifDateTime
was created with microseconds.
So use as simple:
$micro_date = microtime();
$date_array = explode(" ",$micro_date);
$date = date("Y-m-d H:i:s",$date_array[1]);
echo "Date: $date:" . $date_array[0]."<br>";
Recommended and use dateTime()
class from referenced:
$t = microtime(true);
$micro = sprintf("%06d",($t - floor($t)) * 1000000);
$d = new DateTime( date('Y-m-d H:i:s.'.$micro, $t) );
print $d->format("Y-m-d H:i:s.u"); // note at point on "u"
Note u
is microseconds (1 seconds = 1000000 µs).
Another example from php.net:
$d2=new DateTime("2012-07-08 11:14:15.889342");
Reference of dateTime()
on php.net
I've answered on question as short and simplify to author. Please see for more information to author: getting date format m-d-Y H:i:s.u from milliseconds
Date to milliseconds in PHP
I've tried this
date("YmdHisu")
but it returns time to microseconds.
If you don't need the microseconds then just strip them:
substr(date("YmdHisu"), 0, -3);
But, because currently date()
formats the value returned by time()
that doesn't contain fractions of seconds, the result will always end with 000
(no milliseconds available).
You can get the date and the milliseconds as you need if you use date("YmdHis")
to format the date and time part (whole seconds) and extract and concatenate the first 3 digits of the microseconds part returned by microtime()
:
echo(date("YmdHis").substr(microtime(FALSE), 2, 3));
PHP - get milliSecond from Timestamp
You can use microtime (which is better solution because you will get exact value).
Getting unix timestamp in milliseconds in PHP5 and Actionscript3
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á.
converting milliseconds to amount of time string
PHP has a date function which do what you want:
date("H:i:s", '183547165');
It outputs:
09:19:25
PHP DATE
How to convert time in milliseconds in php
$string = "00:38:42,689";
$time = explode(":", $string);
$hour = $time[0] * 60 * 60 * 1000;
$minute = $time[1] * 60 * 1000;
$second = explode(",", $time[2]);
$sec = $second[0] * 1000;
$milisec= $second[1];
$result = $hour + $minute + $sec + $milisec;
echo $result;
Related Topics
How to Sort an Array of Utf-8 Strings
Instantiate a Class with or Without Parentheses
Get Element by Classname with Domdocument() Method
How to Get a Div Content in PHP
Convert PDF to Jpeg with PHP and Imagemagick
Compare Multidimensional Arrays in PHP
Sorting Laravel Collection via Array of Id'S
Mcrypt_Encrypt to Openssl_Encrypt, and Openssl_Zero_Padding Problems
How to Convert JSON to Xml in PHP
Int((0.1+0.7)*10) = 7 in Several Languages. How to Prevent This
Sort an Array by a Child Array's Value in PHP
Laravel Eager Loading with Limit
More Concise Way to Check to See If an Array Contains Only Numbers (Integers)