zoukankan      html  css  js  c++  java
  • PHP生成唯一订单号

    function build_order_no()
    {
        return date('Ymd').substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8);
    }
    
    
    用uniqid获取一个基于当前的微秒数生成的唯一不重复的字符串,取其第8到13位。但是这个字符串里面有英文字母,用ord获取他的ASCII码,用str_split把这个字符串分割为数组,用array_map去操作(速度快点)。
    然后返回的还是一个数组,在用implode弄成字符串,但是字符长度不定,取前固定的几位,然后前面加上当前的年份和日期,这个方法生成的订单号,全世界不会有多少重复的。
    
    public string GetOrderNumber()
    {
                string Number = DateTime.Now.ToString("yyMMddHHmmss");//yyyyMMddHHmmssms
                return Number + Next(1000,1).ToString();
    }
    private static int Next(int numSeeds, int length)
    {
                // Create a byte array to hold the random value.  
                byte[] buffer = new byte[length];
                // Create a new instance of the RNGCryptoServiceProvider.  
                System.Security.Cryptography.RNGCryptoServiceProvider Gen = new System.Security.Cryptography.RNGCryptoServiceProvider();
                // Fill the array with a random value.  
                Gen.GetBytes(buffer);
                // Convert the byte to an uint value to make the modulus operation easier.  
                uint randomResult = 0x0;//这里用uint作为生成的随机数  
                for (int i = 0; i < length; i++)
                {
                    randomResult |= ((uint)buffer[i] << ((length - 1 - i) * 8));
                }
                // Return the random number mod the number  
                // of sides. The possible values are zero-based  
                return (int)(randomResult % numSeeds);
    }
    
    return date('Ymd') . str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT);
    
    return 'SD'.date('Ymd') . str_pad(mt_rand(1, 9999999), 7, '0', STR_PAD_LEFT);
    
  • 相关阅读:
    什么是垃圾回收??
    Nginx教程3:SSL设置
    Nginx教程2:性能
    Nginx教程1:基本概念
    iOS 设计中-- 自定义-- 评星图标的方法
    iOS设计中对Xcode设置中创建PCH文件的过程
    iOS设计中不同屏幕适配的方法-登陆界面
    iOS设计之--OC学习总结之延展类目协议
    iOS 最新版 CocoaPods 的安装流程介绍
    iOS基本UI控件总结
  • 原文地址:https://www.cnblogs.com/zhuiluoyu/p/4674335.html
Copyright © 2011-2022 走看看