6 ways for PHP to generate unique order numbers

Source: Internet
Author: User
Tags ord rand sprintf


Do mall type projects often need to generate a unique order number, the following to a summary!

Method One:

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

Method Two: Is using ...

/**

* *

UNIQID-The official said:

* Gets A prefixed unique identifier based on the "the" in Microseconds.
  /

function build_order_no ()

{return

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


Use Uniqid to get a unique string that is generated based on the current microsecond number (but his top 7 looks like a long time before it changes, so don't worry about deleting it), take the 8th to 13th place. But there are English letters in this string.
Use Ord to get his ASCII code, so there's the next step: Split the string into str_split with the array_map to manipulate it (faster).
And then return an array, KO, in the implode string, but the length of the characters are indefinite, take a few fixed before, and then add the current year and date, the method generated by the order number, the world will not have much duplication.
Of course, unless you put the server time forward, but not to be afraid, brother does not believe he will be in the same microsecond two orders, network data transmission also point time, even if you are local.


Method Three: Used before ...

The Public Function Make_order ($user _id)

{return

Mt_rand (10,99)

. sprintf ('%010d ', Time ()-946656000)

. sprintf ('%03d ', (float) microtime () * 1000)

. sprintf ('%03d ', (int) $user _id% 1000);

Method Four:

$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, 99

));


Method five generates a 24-bit unique order number

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 Main (yyyymmddhhiissnnnnnnnn)     $order _id_main = date (' ymdhis ')  .
 rand (10000000,99999999);
   //Order number main 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)); &NBSP;&NBSP;&NBSP}    //Unique order number (YYYYMMDDHHIISSNNNNNNNNCC)     $order _id =   $order _id_main . str_pad (100 -  $order _id_sum % 100)  % 100,2, ' 0 '
, Str_pad_left);       if ($result  = sqlIte_query ($db, "select * from orders where order_id= ' $order _id")) {     if (Sqlite_num_rows ($result)  == 0) {     break;      }    }

Example Six

/**

* Generates 16-bit pure Digital order number

* Maximum support time to 2056-12-31 23:59:59

* * @access public

* @return

string

* * The public static function Getordersn () {return

(date (' y ') + date (' m ') + date (' d ')). Str_pad (()-Strtotime (date () Y-m-d ')), 5, 0, Str_pad_left). SUBSTR (Microtime (), 2, 6). sprintf ('%03d ', rand (0, 999));

}

Currently in use, the basic guarantee does not repeat, can use until 2056, feel that should be enough, after the need to expand

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.