zoukankan      html  css  js  c++  java
  • php 雪花算法

    <?php
    class Idcreate {
        const EPOCH = 0;
        //开始时间,固定一个小于当前时间的毫秒数
        const max12bit = 1024;
        const max41bit = 1099511627888;
        static $machineId = null;
        public static function machineId($mId = 0) {
            self::$machineId = $mId;
        }
        public static function createOnlyId() {
            // 时间戳 42字节
            $time = floor(microtime(true) * 1000);
            // 当前时间 与 开始时间 差值
            $time -= self::EPOCH;
            // 二进制的 毫秒级时间戳
            $base = decbin(self::max41bit + $time);
            // 机器id  10 字节
            if(!self ::$machineId) {
                $machineid = self ::$machineId;
            } else {
                $machineid = str_pad(decbin(self ::$machineId),10,"0",STR_PAD_LEFT);
            }
            $random = str_pad(decbin(mt_rand(0,self::max12bit)),12,"0",STR_PAD_LEFT);
            // 拼接
            $base = $base . $machineid . $random;
            // 转化为 十进制 返回
            return bindec($base);
        }
    }
    $obj = new Idcreate;
    for ($i=0; $i <100 ; $i++) {
        echo $obj->createOnlyId()."<br>";
    }
  • 相关阅读:
    嵌入式系统引导和启动的流程
    microblaze以太网程序
    机试题
    共模差分 对比
    xilinx XPS无法启动的问题
    FPGA开发流程
    Mel-Frequency-Warping
    微软-黄学东-清华大学FIT楼报告-20170407
    Matlab
    Anaconda安装
  • 原文地址:https://www.cnblogs.com/chengfengchi/p/13274302.html
Copyright © 2011-2022 走看看