How to generate a unique order number in PHP _ PHP Tutorial

Source: Internet
Author: User
A summary of how PHP generates a unique order number. PHP generates a unique order number. The first code is as follows: returndate (Ymd ). str_pad (mt_rand (1,99999), 5, 0, STR_PAD_LEFT); the second code is as follows: returndate (Ymd ). summary of methods for generating unique order numbers in su PHP

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

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, where: YYYY = year, MM = month, DD = date, HH = 24 format hour, II = minute, SS = second, NNNNNNNN = random number, CC = check code

@ Date_default_timezone_set ("PRC ");

While (true ){

// Date of order

$ Order_date = date ('Y-m-D ');

// Subject of the order number (yyyymmddhhi1_nnnnnnn)

$ Order_id_main = date ('ymdhis '). rand (, 9999999999 );

// Order number subject 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 (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.

1

2

3

4

$ 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 first code of lifecycle is as follows: return date ('ymmd '). str_pad (mt_rand (1, 99999), 5, '0', STR_PAD_LEFT); the second code is as follows: return date ('ymmd '). su...

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.