123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- <?php
- namespace app\common\library;
- class SnowFlake
- {
-
- const EPOCH = 1672502400000;
-
- const max41bit = 1099511627775;
-
- static $machineId = 1;
-
- static $count = 0;
-
- static $last = 0;
-
- public static function setMachineId(int $mId)
- {
- self::$machineId = $mId;
- }
- public static function generateParticle()
- {
-
- $time = (int)floor(microtime(true) * 1000);
-
- $time -= self::EPOCH;
-
- $base = decbin(self::max41bit + $time);
-
- if (!is_null(self::$machineId)) {
- $machineId = str_pad(decbin(self::$machineId), 10, "0", STR_PAD_LEFT);
- $base .= $machineId;
- }
-
- if ($time == self::$last) {
- self::$count++;
- } else {
- self::$count = 0;
- }
-
- $sequence = str_pad(decbin(self::$count), 12, "0", STR_PAD_LEFT);
- $base .= $sequence;
-
- self::$last = $time;
-
- return bindec($base);
- }
- }
|