Home  >  Article  >  Backend Development  >  Summary of methods to generate unique order numbers in PHP_PHP tutorial

Summary of methods to generate unique order numbers in PHP_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 09:57:011234browse

A summary of methods for generating unique order numbers in PHP

The first method

The code is as follows:


return date('Ymd') . str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT);

Second type

The code is as follows:


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

Third type

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

//生成24位唯一订单号码,格式:YYYY-MMDD-HHII-SS-NNNN,NNNN-CC,其中:YYYY=年份,MM=月份,DD=日期,HH=24格式小时,II=分,SS=秒,NNNNNNNN=随机数,CC=检查码

 

@date_default_timezone_set("PRC");

 

while(true){

 

//订购日期

 

$order_date = date('Y-m-d');

 

//订单号码主体(YYYYMMDDHHIISSNNNNNNNN)

 

$order_id_main = date('YmdHis') . rand(10000000,99999999);

 

//订单号码主体长度

 

$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));

}

//唯一订单号码(YYYYMMDDHHIISSNNNNNNNNCC)

$order_id = $order_id_main . str_pad((100 - $order_id_sum % 100) % 100,2,'0',STR_PAD_LEFT);

1

2

3

4

5

6

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('d', rand(0, 99));

?>

7 8

9

10

11
12

13

14 15

1617 18 19 20 21 22
23
24
25 26 27 28 29
//Generate a 24-digit unique order number, format: YYYY-MMDD-HHII-SS-NNNN, NNNN-CC, where: YYYY=year, MM=month, DD=date, HH=hour in 24 format, 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 body (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) % 100,2,'0',STR_PAD_LEFT);<🎜> <🎜>
<🎜> <🎜> <🎜> <🎜>The fourth type: <🎜> <🎜>After searching on the Internet, I found that this classmate’s idea is quite good, redtamo. Please check it out for details. I will give a brief overview. This method uses English letters, year, month and day, and Unix timestamp. With microseconds and random numbers, the possibility of repetition is greatly reduced, which is still very good. The use of letters is very representative, one letter corresponds to a year, a total of 16 digits, no more, no less, 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(' d', rand(0, 99));<🎜> <🎜>?>
Generation effect: The code is as follows: A422694333616096 Alas, it’s a pity that this method was not used in the final project. It was said that there was no need to make it so complicated, - -! http://www.bkjia.com/PHPjc/985147.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/985147.htmlTechArticleA summary of the methods for PHP to generate unique order numbers. The first code is as follows: return date('Ymd') . str_pad( mt_rand(1, 99999), 5, '0', STR_PAD_LEFT); The second code is as follows: return date('Ymd').su...
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