标签:
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);
}
标签:
原文地址:http://www.cnblogs.com/zhuiluoyu/p/4674335.html