Summary of how PHP generates a unique order number

Source: Internet
Author: User
Tags rand

First Kind

The

code is as follows:


return date (' Ymd '). Str_pad (Mt_rand (1, 99999), 5, ' 0 ', str_pad_left);

Second Kind

The

code is as follows:


return date (' Ymd '). substr (Implode (NULL, Array_map (' Ord '), Str_split (substr (Uniqid (), 7, 13), 1)), 0, 8);

Third Kind

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28-29 Generate 24-bit unique order number, format: YYYY-MMDD-HHII-SS-NNNN,NNNN-CC, which: yyyy= year, mm= month, dd= date, hh=24 format hours, ii= minutes, ss= seconds, nnnnnnnn= random number, cc= check code   @date_default_timezone_set ("PRC");   while (true) {//order date $order _date = Date (' y-m-d '); Order number Principal (yyyymmddhhiissnnnnnnnn) $order _id_main = Date (' Ymdhis ').   Rand (10000000,99999999);   Order number Body length $order _id_len = strlen ($order _id_main);   $order _id_sum = 0;   For ($i =0 $i < $order _id_len $i + +) {$order _id_sum + = (int) (substr ($order _id_main, $i, 1)); //Unique order number (YYYYMMDDHHIISSNNNNNNNNCC) $order _id = $order _id_main. Str_pad ((100-$order _id_sum%)% 100,2, ' 0 ', str_pad_left);

The fourth kind:

Looking for it on the internet, found this classmate's idea is very good, Redtamo, concrete Please steady past look, I make a brief overview, the method used in English alphabet, years, Unix time stamp and microsecond number, random number, the likelihood of duplication is greatly reduced, or very good. The use of letters is very representative, a letter corresponding to a year, a total of 16, not many also a lot, hehe.

1 2 3 4 <?php $yCode = Array (' A ', ' B ', ' C ', ' D ', ' E ', ' F ', ' G ', ' H ', ' I ', ' J '); $orderSn = $yCode [Intval (Date (' Y ')]-2011]. Strtoupper (Date (' M ')) (Dechex). Date (' d '). SUBSTR (Time (),-5). SUBSTR (Microtime (), 2, 5). sprintf ('%02d ', rand (0));?>

Build Effect:

code is as follows: A422694333616096

Alas, unfortunately, the final project did not use this method, said is not necessary to make such a complex,-!

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.