datacenter_id = $datacenter_id; $this->machine_id = $machine_id; } /** * Generate an unique ID based on SnowFlake * * @return string Unique ID */ public function generateID() { $sign = 0; // default 0 $time = (int)($this->getUnixTimestamp() - self::EPOCH_OFFSET); $sequence = $this->getNextSequence($time); $this->lastTime = $time; $id = ($sign << 1) | ($time << 41) | ($this->datacenter_id << 5) | ($this->machine_id << 5) | ($sequence << 12) ; return (string)$id; } /** * Get UNIX timestamp in microseconds * * @return int Timestamp in microseconds */ private function getUnixTimestamp() { return floor(microtime(true) * 1000); } /** * Get the next sequence number if $time * was already used * * @param int $time (micro)timestamp from EPOCH_OFFSET * @return int Sequence number */ private function getNextSequence($time) { if($time === $this->lastTime) { return ++$this->sequence; } return $this->sequence; } }