创建一个日期 + 字符的用户名

说明:头部为日期,一天的时间戳是 86400s,再加上微妙前两位,对于小网站基本用户名不会重复了。
生成规则 日期 20200425 + (时间戳后 5 位 + microtime 微秒前 2 位)转 62 进制

public static function crateUserName($prefix=''){
        $microtime = microtime(true); // 1587822735.9332
        list($timestamps,$micro) = explode('.',$microtime);
        $head = date('Ymd',$timestamps);//20200425
        $end_number = substr($timestamps,-5).substr($micro,0,2);//2273593
        $end_str = self::ten2SixtyTwo($end_number);// 9yB5
        return $prefix.$head.$end_str; //202004259yB5
    }

 protected static function ten2SixtyTwo(int $num){
        $base='0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
        $result='';
        while($num!=0){
            $result=$base[$num%62].$result;
            $num=intval($num/62);
        }
        return $result;
    }
php
本作品采用《CC 协议》,转载必须注明作者和本文链接
welcome come back
未填写
文章
98
粉丝
25
喜欢
160
收藏
360
排名:316
访问:3.0 万
私信
所有博文
社区赞助商