高并发的情况下生成不重复的订单号

2020-06-06  本文已影响0人  zlchen

根据时间生成订单号

/**
 * 生成订单号
 * @User zlchen
 * @param int $length
 * @param string $tab
 * @return string
 */
function getOrderNum($length = 6, $tab = '')
{
    //20200606182556
    $dt = date('YmdHis');
    
    //5edb6bd-06f1a6  5edb6bd-292268   后6位不同
    $uniqid =  uniqid();
    
    //字符串截取 47b803   
    $str_7_13 =  substr($uniqid, 7, 13);
    
    //str_split ( string $string [, int $split_length = 1 ] ) : array  str_split — 将字符串转换为数组
    $str_to_array = str_split($str_7_13, 1);
    
    //(PHP 4, PHP 5, PHP 7)  ord — 转换字符串第一个字节为 0-255 之间的值
    $arr = array_map('ord', $str_to_array); //[52,55,98,56,48,51]
    
    //implode — 将一个一维数组的值转化为字符串
    $arr_to_string =  implode(NULL, $arr);  //531015452102102
    
    //implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1)))  
    //20200606182903102545
    $str = $dt . substr($arr_to_string, 0, $length);
    
    return $str . $tab;
 
}
//20200606182903102545zlchen
getOrderNum(6, 'zlchen');
上一篇下一篇

猜你喜欢

热点阅读