PHPackages                             wantp/snowflake - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. wantp/snowflake

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

wantp/snowflake
===============

A package for create unique id by snowflake

v1.2.0(6y ago)399.6k↓73.6%141MITPHPPHP &gt;=7.0.0

Since Dec 26Pushed 6y ago1 watchersCompare

[ Source](https://github.com/wantp/snowflake)[ Packagist](https://packagist.org/packages/wantp/snowflake)[ RSS](/packages/wantp-snowflake/feed)WikiDiscussions master Synced 2d ago

READMEChangelogDependenciesVersions (4)Used By (1)

snowflake
=========

[](#snowflake)

Twitter雪花算法snowflake的PHP实现，解决并发可能造成id重复的问题

#### 概述

[](#概述)

- snowflake算法生成的19个字符长度的唯一数字id
- 由于php-fpm多进程的特性,并发下可能造成id重复,提供redis和文件锁两种方式来控制并发下的id重复,默认使用文件锁形式
- 生成的id基于时间趋势增长
- 支持生成时间到2080-09-05
- 支持32个数据中心,32台机器
- 每毫秒内可以生成4096个不同id

#### snowflake算法简述

[](#snowflake算法简述)

##### 使用64bit来标识一个唯一id，生成规则:

[](#使用64bit来标识一个唯一id生成规则)

> 1bit正负标识位 - 41bits毫秒级时间戳 - 5bits数据中心id - 5bits机器id -12bits毫秒内顺序id

##### 使用说明

[](#使用说明)

###### 引用包

[](#引用包)

```
composer require wantp/snowflake

```

###### 生成id

[](#生成id)

```
require_once 'vendor/autoload.php';

$IdWorker = \wantp\Snowflake\IdWorker::getIns();
$id = $IdWorker->id();

```

###### 反向解析id

[](#反向解析id)

```
$idInfo = $IdWorker->parse($id);

```

###### 分布式，设置机器id

[](#分布式设置机器id)

```
$dataCenterId = 2;
$machineId = 5;
$IdWorker = \wantp\Snowflake\IdWorker::getIns($dataCenterId,$machineId);
$id = $IdWorker->id();

```

###### 使用redis来控制并发

[](#使用redis来控制并发)

- 需要先安装配置好redis，设置redis时需要填写redis配置
- redis配置说明

key是否必填说明host是redis主机port是redis端口dbIndex否redis db indexauth否redis认证```
$resdisConfig = ['host'=>'redis host','port'=>'redis port','dbIndex'=>'redis dbIndex',auth'=>'redis auth'];
$IdWorker = \wantp\Snowflake\IdWorker::getIns()->setRedisConutServer($resdisConfig);
$id = $IdWorker->id();

```

###  Health Score

35

—

LowBetter than 77% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity36

Limited adoption so far

Community14

Small or concentrated contributor base

Maturity58

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~111 days

Total

3

Last Release

2525d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/505a2d2932f58f4ae41d25f78468b7ce37ce2af76e27a5580d3ce3fefcf068ee?d=identicon)[wantp](/maintainers/wantp)

---

Top Contributors

[![wayfarer35](https://avatars.githubusercontent.com/u/36877026?v=4)](https://github.com/wayfarer35 "wayfarer35 (6 commits)")

---

Tags

snowflakeuniqueId

### Embed Badge

![Health badge](/badges/wantp-snowflake/health.svg)

```
[![Health](https://phpackages.com/badges/wantp-snowflake/health.svg)](https://phpackages.com/packages/wantp-snowflake)
```

###  Alternatives

[kra8/laravel-snowflake

Snowflake for Laravel and Lumen.

192453.6k9](/packages/kra8-laravel-snowflake)[hyperf/snowflake

A snowflake library

27853.3k70](/packages/hyperf-snowflake)[identifier/identifier

Common Interfaces and Factories for Identifiers

3233.4k1](/packages/identifier-identifier)[ramsey/identifier

A PHP library for generating and working with identifiers, including UUIDs, ULIDs, and Snowflakes

606.9k2](/packages/ramsey-identifier)[hafael/laraflake

Laravel Package based on Twitter Snowflake ID Generator (64 bit ID).

288.1k1](/packages/hafael-laraflake)[infocyph/uid

UUID (RFC 9562), ULID, Snowflake ID, Sonyflake ID, and TBSL generator for PHP.

116.0k](/packages/infocyph-uid)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
