PHPackages                             zzbajie/yii2-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. zzbajie/yii2-snowflake

ActiveYii2-extension[Utility &amp; Helpers](/categories/utility)

zzbajie/yii2-snowflake
======================

yii2-snowflake

00PHP

Since Apr 1Pushed 4y ago1 watchersCompare

[ Source](https://github.com/zzbajie/yii2-snowflake)[ Packagist](https://packagist.org/packages/zzbajie/yii2-snowflake)[ RSS](/packages/zzbajie-yii2-snowflake/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependenciesVersions (1)Used By (0)

yii2-snowflake
==============

[](#yii2-snowflake)

yii2-snowflake

Installation
------------

[](#installation)

The preferred way to install this extension is through [composer](http://getcomposer.org/download/).

Either run

```
php composer.phar require --prefer-dist zzbajie/yii2-snowflake "*"

```

or add

```
"zzbajie/yii2-snowflake": "*"

```

to the require section of your `composer.json` file.

Usage
-----

[](#usage)

Once the extension is installed, simply use it in your code by :

```
//生成订单号
$redis = Yii::$app->redis;
$redis->select(9);
$snowflake = new Snowflake($redis, Yii::$app->params['snowFlake']['machineId'], Yii::$app->params['snowFlake']['startTime'], 'order');
return (string)$snowflake->generate();
```

snowflake
=========

[](#snowflake)

该版本使用到了PHP7语法，并使用REDIS来作为锁机制

### 算法介绍

[](#算法介绍)

snowflake 是一种分布式ID生成器，Twitter出品，通过加入时间戳相对值、数据标志、机器标志、递增序列号，保证id的有序及不重复。

符号位（1bit）- 时间戳相对值（41bit）- 数据标志（5bit）- 机器标志（5bit）- 递增序号（12bit） 0 - 0000000000 0000000000 0000000000 0000000000 0 - 00000 - 00000 - 000000000000

### 本版概述

[](#本版概述)

- 本版将数据标识、机器标识合二为一统称为机器标识。即：10bit，0-1023
- 本版使用 REDIS 来确保单位毫秒内ID保持自增并防止并发重复

### 其它补充

[](#其它补充)

- ID长度：
    - 标准算法ID长度为18或19位。当时间差超过7.5年左右，就会达到19位
- 机器标识：
    - 机器标识可以写到php-fpm.conf中。如：`env[MACHINE_ID]=10`。待修改完成并重启php-fpm后即可在项目中使用`getenv('MACHINE_ID')`获取机器标识

###  Health Score

14

—

LowBetter than 2% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity0

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity26

Early-stage or recently created project

 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.

### Community

Maintainers

![](https://www.gravatar.com/avatar/f4d53f33395a6330d27e45be04baf8449aa670e07bf894b93b7d4d90af72b3f2?d=identicon)[zzbajie](/maintainers/zzbajie)

---

Top Contributors

[![zzbajie](https://avatars.githubusercontent.com/u/38753679?v=4)](https://github.com/zzbajie "zzbajie (2 commits)")

### Embed Badge

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

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

###  Alternatives

[riverskies/laravel-vue-component

Helper package to aid usage of Vue Components within Laravel projects

201.2k](/packages/riverskies-laravel-vue-component)

PHPackages © 2026

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