5 ways to generate unique order numbers in php

不言
Release: 2023-04-02 13:42:02
Original
28762 people have browsed it

This article mainly introduces the method of generating a unique order number in php, which has a certain reference value. Now I share it with you. Friends in need can refer to it

php generates a unique order number 5 methods

The first one

private function doCreateOrderNumber($time){
          $i=1;
          $dd = date('Ymd',$time);
          $aa = 'OH'.$dd;
          $res = $this->orderModel->query("select sn from sr_order_list where sn like '$aa%' order by id limit 1");
          if(!isset($res[0]['sn'])){
                $i = 1;
          }else{
                $i = (int)substr($res[0]['sn'],9,10) + 1;
          }
          while(true){
                $nsn = 'OH'.$dd.$i;
                $exist = $this->orderModel->query("select id from sr_order_list where sn = '$nsn' ");
                if($exist){
                      $i++;
                }else{
                      return $nsn;
                }
          }
    }
Copy after login

The second one

$osn = date('Ymd') . str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT);
echo $osn; //2018070462577
Copy after login

The third one

$osn = date('Ymd').substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8);
echo $osn; //2018070499495653
Copy after login

The fourth one

$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));
  }
  $osn = $order_id_main . str_pad((100 - $order_id_sum % 100) % 100,2,&#39;0&#39;,STR_PAD_LEFT);
echo $osn; //201807041332258742313727
Copy after login

Fifth Type

$code = array(&#39;A&#39;, &#39;B&#39;, &#39;C&#39;, &#39;D&#39;, &#39;E&#39;, &#39;F&#39;, &#39;G&#39;, &#39;H&#39;, &#39;I&#39;, &#39;J&#39;);
$osn = $code[intval(date(&#39;Y&#39;)) - 2011] . strtoupper(dechex(date(&#39;m&#39;))) . date(&#39;d&#39;) . substr(time(), -5) . substr(microtime(), 2, 5) . sprintf(&#39;%02d&#39;, rand(0, 99));
echo $osn; //H704764673624352
Copy after login

The above is the entire content of this article. I hope it will be helpful to everyone's study. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

php code to implement WeChat payment

##Introduction to installing xhprof for performance analysis in PHP 7.1

The above is the detailed content of 5 ways to generate unique order numbers in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!