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);
    
  • 相关阅读:
    Unity shader 代码高亮+提示
    PTA --- L2-003 月饼
    PTA --- L2-002 链表去重
    计蒜客 —— 字符串p型编码
    计蒜客 —— 最好的草
    最近忙科研立项 & 对博客的优化
    计蒜客 —— 删除单词后缀
    Tensorflow 保存模型 & 在java中调用
    Tensorflow 用训练好的模型预测
    Tensorflow 保存和载入训练过程
  • 原文地址:https://www.cnblogs.com/zhuiluoyu/p/4674335.html
Copyright © 2011-2022 走看看