PHP method for generating unique order numbers

Source: Internet
Author: User
There was an e-commerce project a while ago and an order number needs to be generated. At that time, it was easy to consider adding a random number to the system time or using the uniqid () method. Carefully consider the above method, when the customer buys less, order

There was an e-commerce project a while ago and an order number needs to be generated. At that time, it was easy to consider adding a random number to the system time or using the uniqid () method. Carefully consider the above method, when the customer buys less, order

First

The Code is as follows:


Return date ('ymmd'). str_pad (mt_rand (1, 99999), 5, '0', STR_PAD_LEFT );

Second

The Code is as follows:


Return date ('ymmd '). substr (implode (NULL, array_map ('ord ', str_split (substr (uniqid (), 7, 13), 1), 0, 8 );

Third

// Generate 24-digit unique order number, format: YYYY-MMDD-HHII-SS-NNNN, NNNN-CC, where: YYYY = year, MM = month, DD = date, HH = 24 format hour, II = minute, SS = Second, nnnnnnnnnn = random number, CC = check code @ date_default_timezone_set ("PRC"); while (true) {// order date $ order_date = date ('Y-m-d'); // order number subject (yyyymmddhhi1_nnnnnnn) $ order_id_main = date ('ymdhis '). rand (, 99999999); // length of the subject of the order number $ 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 (yyyymmddhhi1_nnnnnnncc) $ order_id = $ order_id_main. str_pad (100-$ order_id_sum % 100) %, 2, '0', STR_PAD_LEFT );

Fourth:

After searching for a redtamo on the internet, I found this student had a good idea. Please take a look at the details. I will give a brief overview, this method uses English letters, year, month, and day, Unix timestamps, microseconds, and random numbers. The possibility of repetition is greatly reduced, which is quite good. The use of letters is very representative, a letter corresponds to a year, a total of 16 characters, not many also a lot, haha.

<? Php $ yCode = array ('A', 'B', 'C', 'D', 'E', 'F', 'G', 'h ', 'I', 'J'); $ orderSn = $ yCode [intval (date ('y')-2011]. strtoupper (dechex (date ('M '))). date ('D '). substr (time (),-5 ). substr (microtime (), 2, 5 ). sprintf ('% 02d', rand (0, 99);?>

Effect:

The Code is as follows:

A422694333616096

Alas, but unfortunately, this method is not used in the final project. It is said that it is not necessary to make such a complicated project ,--!

The above four methods are all shared in this article. I hope you will like them.

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.