Home > Backend Development > PHP Problem > How to get the current millisecond timestamp in php

How to get the current millisecond timestamp in php

coldplay.xixi
Release: 2023-03-06 10:14:01
Original
19861 people have browsed it

php method to get the current millisecond timestamp: use the [microtime()] function to get it, the code is [list($msec, $sec) = explode(' ', microtime());].

How to get the current millisecond timestamp in php

php method to get the current millisecond timestamp:

provides a microtime() function. If called Without optional parameters, this function returns a string in the format "msec sec", where sec is the number of seconds since the Unix epoch (0:00:00 January 1, 1970 GMT) and msec is the microsecond portion . Both parts of the string are returned in seconds.

Detailed description of microtime() function

<?php
echo microtime();
//输出结果是
//0.25139300 1138197510
Copy after login

Note that its result is divided into two parts, that is, the first half is milliseconds (but the unit is seconds) , the second half is seconds.

Now, we make modifications based on this, as follows:

<?php
list($msec, $sec) = explode(&#39; &#39;, microtime());
$msectime = (float)sprintf(&#39;%.0f&#39;, (floatval($msec) + floatval($sec)) * 1000);
Copy after login

That’s it, $msectime is the current number of milliseconds! These two lines can be encapsulated into a function for easy use.

<?php
//返回当前的毫秒时间戳
function msectime() {
  list($msec, $sec) = explode(&#39; &#39;, microtime());
  $msectime = (float)sprintf(&#39;%.0f&#39;, (floatval($msec) + floatval($sec)) * 1000);
  return $msectime;
}
Copy after login

Note: sprintf('%.0f', $num) outputs a floating point number without a decimal part

The thing is not over yet. After I changed the timestamp to millisecond level, When the database data was updated again, it was prompted that it was out of range. It turned out that I used int type to store the second-level timestamp obtained by the time() function in the database. The storage range was sufficient. If I changed it to millisecond level, I had to change it. It is of BIGINT type.

integer type byte range (with symbols) range (non-symbol) use

Tinyint 1 byte (-128, 127) (0,255) small integer Value

SMALLINT                   2 bytes                                                                                                         ( 65 535)                                               ’ to 3 byte       through ’’’’ out's ’ ’ s ’ through   ’ through through ’’'' through'''' through through‐‐to‐to‐‐‐under‐ coming to 3 bytes to be 88 608, 8 388 607) (0 , 16 777 215) Large integer value

INT or INTEGER 4 bytes (-2 147 483 648, 2 147 483 647) (0, 4 294 967 295) Large integer value

BIGINT 8 bytes (-9 233 372 036 854 775 808, 9 223 372 036 854 775 807) (0, 18 446 744 073 709 551 615) Very large integer value

Want to know more about programming To learn, please pay attention to the
php training

column!

The above is the detailed content of How to get the current millisecond timestamp in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template