Two methods of generating order number in PHP

web front end seven thousand one hundred and sixty-six 7 years ago (2017-03-18)

When developing a project, there is often a need to generate an order number. Here are two common methods to generate a 20 digit order number.


Method 1:

The following code is a 14 digit current time plus 6 random numbers. If you want to increase or decrease the number of digits, just change the number 6 at the end.

 <? php function build_order_no(){     return date('YmdHis').substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 6); } echo build_order_no(); ?>


Method 2:

The following code is 14 bits of the current time plus 6 bits of random number. If you increase or decrease the number of bits, you can modify the minimum and maximum number of bits in the random function rand.

 <? php function getOrderId(){ date_default_timezone_set('Asia/Shanghai'); $time= date('YmdHis',time()); $randsix = rand('100000','999999'); return $time.$ randsix; } echo getOrderId(); ?>