Home  >  Article  >  php教程  >  php generates unique order number

php generates unique order number

WBOY
WBOYOriginal
2016-08-26 10:12:481146browse

Support changing length
/**
* Generate unique order number
*
*/function build_order_no(){<br> Return date('Ymd').substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8);<br> }Note:
Use uniqid to obtain a unique and unique string generated based on the current number of microseconds (but the first 7 digits seem to change for a long time, so you can delete it without considering it), and take the 8th to 13th digits. But there are English letters in this string, what should I do?
Use ord to get his ASCII code, so there is the next step: use str_split to split the string into an array, and use array_map to operate (faster).
Then what is returned is still an array, KO, which is converted into a string using implode, but the character length is variable. Take the first fixed few digits, and then add the current year and date in front. The order number generated by this method will not be used in the world. How many duplicates are there.
Of course, unless you adjust the server time forward, don't be afraid to adjust it. I don't believe that he will place two orders in the same microsecond. Network data transmission also takes time, even if you are local.

Statement:
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