PHP gets timestamps and microseconds and generates unique IDs

Source: Internet
Author: User
Tags unique id

Microtime function

Description: Returns the current UNIX timestamp and number of microseconds

Syntax: Mixed microtime ([bool $get _as_float])

Direct output echo microtime ();//Get the following: 0.26672100 1585622985 Front is the current microsecond number, followed by the normal timestamp, separated by a space in the middle//If with parameters (Boolean parameter) echo Microtime (True) ;//Output result: 1585623020.7408 The number of microseconds that have just been shown in the form of floating-point numbers after the original timestamp

Floating-point numbers are too long for formatting operations

Round ($float, 3) means reserved to three digits after the decimal point

uniqid function

Description: Generate a unique ID

Syntax: String uniqid ([String $prefix = "" [, bool $more _entropy = false]) is an optional parameter

The first parameter is a prefix

Echo uniqid ("haha"); // The result is haha ... (randomly generated later)

But we don't have the same prefix that doesn't make sense, so we can

Uniqid (Microtime ());

Uniqid (Microtime (). Mt_rand ());

This time we will find that it will have time stamp and random number + random ID, but the length is not fixed

At this time we can use UUID (Generate unique ID)

The UUID 8-4-4-4-12 is exactly 32 bits (MD5 is 32 bits)

echo MD5 (Uniqid (Microtime (). Mt_rand ())), ' <br> ';

This produces a form of UUID, so that the file name even in a distributed environment is unique, some sites will also use the MAC address of the network card, this is the only;

PHP gets timestamps and microseconds and generates unique IDs

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.