Home > Article > Backend Development > Summary of methods to generate unique order numbers in PHP_PHP tutorial
The first method
The code is as follows:
Second type
The code is as follows:
Third type
3 4 5
6
9 10 11
12
13 14 15 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);<🎜> <🎜> |
<🎜>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));<🎜> <🎜>?> |