PHP get timestamp and microseconds and generate unique id

microtime function

Description: Returns the current Unix timestamp and microseconds

语法:mixed microtime( [ bool $get_as_float ] )

//direct output
echo microtime();
//The result is such as: 0.26672100 1585622985 The front is the current microseconds, followed by the normal timestamp, separated by spaces

//if with parameters (boolean parameters)
echo microtime(true);
//Output result: 1585623020.7408 The number of microseconds just now will be expressed in the form of floating point numbers after the original timestamp

Floating-point numbers are too long to be formatted

round($float,3) means reserved to three decimal places

 

uniqid function

Description: Generate a unique ID

Syntax: string uniqid( [ string $prefix = " " [, bool $more_entropy = false ]] ) are optional parameters

 

The first parameter is the prefix

echo  uniqid ("haha" );
 // The result is haha... (the latter is randomly generated)

But it doesn't make sense if our prefix is ​​unchanged, so we can

uniqid( microtime() );

uniqid( microtime() . mt_rand() );

At this time, we will find that it will have timestamp and random number + random ID, but the length is not fixed

At this time we can use uuid (to generate a unique ID)

The form of uuid 8-4-4-4-12 is exactly 32 bits (MD5 is 32 bits)

echo md5(uniqid(microtime() . mt_rand())),'<br>';

This generates a UUID form, which ensures that the file name is unique even in a distributed environment. Some websites will use the mac address of the network card, which is also unique;

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325147023&siteId=291194637