PHP implements method to obtain current millisecond timestamp

小云云
Release: 2023-03-20 17:44:01
Original
2460 people have browsed it

This article mainly introduces you to the method of obtaining the current millisecond timestamp in PHP. I hope it can help you.

Recently I am working on the backend of a smart home project. It is necessary to upload the user's configuration information of smart devices to the server in real time in order to achieve synchronization. Therefore, the accuracy of the time is relatively high. At first, I used PHP directly. The time() function is used to obtain the timestamp. The time obtained is accurate to the second level. If the clients operate at the same time, conflicts may still occur. Therefore, it is recommended to increase the timestamp accuracy to the millisecond level. However, there is no built-in function in PHP to obtain it. Millisecond timestamp, but a microtime() function is provided. If called without optional parameters, this function returns a string in the format of "msec sec", where sec is the Unix epoch since 0:00:00 January 1 , 1970 GMT) since now, msec is the microsecond part. Both parts of the string are returned in seconds.

Case:


  1. #//Return the current millisecond timestamp

  2. function msectime() {

  3. list($msec, $sec) = explode(' ', microtime());

  4. $msectime = (float)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000);

  5. }

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

    The matter is not over yet. After I changed the timestamp to millisecond level, when I updated the database data again, it prompted an out-of-range message. It turns out that I used int type to store the time() function in the database. The storage range for timestamps at the second level is sufficient. If you change it to the millisecond level, you have to change it to the BIGINT type.

    Integer type Bytes Range (signed) Range (unsigned) Usage

    TINYINT 1 byte (-128,127) (0,255) Small integer value

    SMALLINT 2 bytes (-32 768, 32 767) (0, 65 535) Large integer value

    MEDIUMINT 3 bytes (-8 388 608, 8 388 607)               (0, 16 777 215)                                                      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) Maximum integer value

Related recommendations:

PHP method to generate millisecond timestamp

The above is the detailed content of PHP implements method to obtain current millisecond timestamp. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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