Home  >  Article  >  php教程  >  生成序列号(把12位时间数值压缩成7-8位字母+数字组合字符串)

生成序列号(把12位时间数值压缩成7-8位字母+数字组合字符串)

PHP中文网
PHP中文网Original
2016-05-23 08:39:382216browse

php代码

/**
 * 生成序列号
 * 
把12位时间数值压缩成7-8位字母+数字组合字符串,重点是加上用户编号后将永不重复哈
 * 
用法:$serial_no = sofn_generate_serial('KH' . $this->user_login_data['id']);
 * 
示例:160121054346(12位,date('ymdhis'))压缩后QBVF4346(8位,sofn_generate_serial())
 * @param string $serial_no 序号前缀,如:'KH' . $this->user_login_data['id']
 * @return string 如:QBVF295
 * @since VER:1.0; DATE:2016-1-21; AUTHOR:SoChishun; EMAIL:14507247@qq.com; DESC:Added.
 */
function sofn_generate_serial($serial_no='') {
    $time = date('y-m-d-H-i-s');
    $atime = explode('-', $time);
    foreach ($atime as $stime) {
        $itime = $stime * 1;
        if ($itime < 26) {
            // 65(A)-90(Z)
            $serial_no.=chr(65 + $itime);
            continue;
        }
        // 48(0)-57(9)
        if ($itime >= 48 && $itime <= 57) {
            $serial_no.=chr($stime);
            continue;
        }
        $serial_no.=$stime;
    }
    return $serial_no;
}

Statement:
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
Previous article:混合字符串函数Next article:常用的检查函数