PHPackages                             guanhui07/php-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. guanhui07/php-snowflake

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

guanhui07/php-snowflake
=======================

An ID Generator for PHP based on Snowflake Algorithm (Twitter announced).

3.1.0(2y ago)02MITPHP

Since Aug 12Pushed 1y agoCompare

[ Source](https://github.com/guanhui07/php-snowflake)[ Packagist](https://packagist.org/packages/guanhui07/php-snowflake)[ Docs](https://github.com/godruoyi/php-snowflake)[ Fund](https://images.godruoyi.com/wechat.png)[ RSS](/packages/guanhui07-php-snowflake/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (6)Versions (26)Used By (0)

 [![](https://camo.githubusercontent.com/d0a5c4d460ca256f5829eb93f2ccae01706a13f8c0322495523a76bc13a149c4/68747470733a2f2f7777772e706e676b65792e636f6d2f706e672f66756c6c2f3130352d313035323233355f736e6f77666c616b652d706e672d7472616e73706172656e742d6261636b67726f756e642d736e6f77666c616b652d776974682d636c6561722d6261636b67726f756e642e706e67)](https://camo.githubusercontent.com/d0a5c4d460ca256f5829eb93f2ccae01706a13f8c0322495523a76bc13a149c4/68747470733a2f2f7777772e706e676b65792e636f6d2f706e672f66756c6c2f3130352d313035323233355f736e6f77666c616b652d706e672d7472616e73706172656e742d6261636b67726f756e642d736e6f77666c616b652d776974682d636c6561722d6261636b67726f756e642e706e67)

An ID Generator for PHP based on Snowflake Algorithm (Twitter announced).

 [ ![build passed](https://github.com/godruoyi/php-snowflake/actions/workflows/test.yml/badge.svg) ](https://github.com/godruoyi/php-snowflake/actions/workflows/test.yml) [ ![](https://camo.githubusercontent.com/bce16616bb61d16e912413ea83afc2edb843013b179ea033c5a07e7adc736f34/68747470733a2f2f636f6465636f762e696f2f67682f676f6472756f79692f7068702d736e6f77666c616b652f6272616e63682f6d61737465722f67726170682f62616467652e7376673f746f6b656e3d3741414f59434a4b3937) ](https://codecov.io/gh/godruoyi/php-snowflake) [ ![License](https://camo.githubusercontent.com/3853f61c85030efe13f2cf109e999af1695e57d90c3181129f4db1aaa13435b6/68747470733a2f2f706f7365722e707567782e6f72672f676f6472756f79692f7068702d736e6f77666c616b652f6c6963656e7365) ](https://github.com/godruoyi/php-snowflake) [ ![Packagist Version](https://camo.githubusercontent.com/e71f6e23e57c7b575c219f1acc63ac522d82dc7183d79b349c7c5749e8a8d18a/68747470733a2f2f706f7365722e707567782e6f72672f676f6472756f79692f7068702d736e6f77666c616b652f762f737461626c65) ](https://packagist.org/packages/godruoyi/php-snowflake) [ ![Total Downloads](https://camo.githubusercontent.com/10fa4f9c82a194b25b72c00ae26bc39e965b131cdaffb634ab13ff0c83f30b24/68747470733a2f2f706f7365722e707567782e6f72672f676f6472756f79692f7068702d736e6f77666c616b652f646f776e6c6f616473) ](https://github.com/godruoyi/php-snowflake)

Description
-----------

[](#description)

Snowflake &amp; Sonyflake algorithm PHP implementation [中文文档](https://github.com/godruoyi/php-snowflake/blob/master/README-zh_CN.md).

[![file](https://camo.githubusercontent.com/c595c9383689900c37f170a0fe41913a76a8c3bd3cc6434c308db66746cbe9ad/68747470733a2f2f696d616765732e676f6472756f79692e636f6d2f6c6f676f732f3230313930382f31332f5f313536353637323632315f4c5057363550693863472e706e67)](https://camo.githubusercontent.com/c595c9383689900c37f170a0fe41913a76a8c3bd3cc6434c308db66746cbe9ad/68747470733a2f2f696d616765732e676f6472756f79692e636f6d2f6c6f676f732f3230313930382f31332f5f313536353637323632315f4c5057363550693863472e706e67)

Snowflake is a network service that generates unique ID numbers at high scale with simple guarantees.

1. The first bit is an unused sign bit.
2. The second part consists of a 41-bit timestamp (in milliseconds) representing the offset of the current time relative to a certain reference time.
3. The third and fourth parts are represented by 5 bits each, indicating the data centerID and workerID. The maximum value for both is 31 (2^5 -1).
4. The last part consists of 12 bits, which represents the length of the serial number generated per millisecond per working node. A maximum of 4095 IDs can be generated in the same millisecond (2^12 -1).

If you want to generate unique IDs using the snowflake algorithm, you must ensure that sequence numbers generated within the same millisecond on the same node are unique. Based on this requirement, we have created this package which integrates multiple sequence number providers.

- RandomSequenceResolver (Random Sequence Number, UnSafe)
- FileLockResolver (Uses PHP file lock `fopen/flock`, **Concurrency Safety**)
- RedisSequenceResolver (Redis psetex and incrby, **Concurrency safety**)
- PredisSequenceResolver (redis psetex and incrby, **Concurrency Safety**)
- LaravelSequenceResolver (Laravel Cache [add](https://github.com/laravel/framework/blob/11.x/src/Illuminate/Contracts/Cache/Repository.php#L39) lock mechanism)
- SwooleSequenceResolver (swoole\_lock for **Concurrency Safety**)

Requirement
-----------

[](#requirement)

1. PHP &gt;= 8.1
2. **[Composer](https://getcomposer.org/)**

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

[](#installation)

```
$ composer require godruoyi/php-snowflake -vvv

# Install `predis/predis` package if you are using PredisSequenceResolver
$ composer require "predis/predis"

# Install `Redis` extensions if you are using RedisSequenceResolver
$ pecl install redis

# Install `Swoole` extensions if you are using SwooleSequenceResolver
$ pecl install swoole
```

Usage
-----

[](#usage)

1. simple to use.

```
$snowflake = new \Godruoyi\Snowflake\Snowflake;

$snowflake->id();
// 1537200202186752
```

2. Specify the data center ID and machine ID.

```
$snowflake = new \Godruoyi\Snowflake\Snowflake($datacenterId, $workerId);

$snowflake->id();
```

3. Specify start time.

```
$snowflake = new \Godruoyi\Snowflake\Snowflake;
$snowflake->setStartTimeStamp(strtotime('2019-09-09')*1000); // millisecond

$snowflake->id();
```

> The maximum value of a 41-bit timestamp (in milliseconds) can represent up to 69 years, so the Snowflake algorithm can run safely for 69 years. In order to make the most of it, we recommend setting a start time.

4. Use Sonyflake

```
$sonyflake = new \Godruoyi\Snowflake\Sonyflake;

$sonyflake->id();
```

Advanced
--------

[](#advanced)

1. Used in Laravel.

Since the SDK is quite straightforward, we do not offer a specific extension for Laravel. However, you can easily integrate it into your Laravel project by following these steps.

```
// App\Providers\AppServiceProvider

use Godruoyi\Snowflake\Snowflake;
use Godruoyi\Snowflake\LaravelSequenceResolver;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        $this->app->singleton('snowflake', function ($app) {
            return (new Snowflake())
                ->setStartTimeStamp(strtotime('2019-10-10')*1000)
                ->setSequenceResolver(new LaravelSequenceResolver($app->get('cache.store')));
        });
    }
}
```

2. Custom

To customize the sequence number resolver, you need to implement the Godruoyi\\Snowflake\\SequenceResolver interface.

```
class YourSequence implements SequenceResolver
{
    /**
     *  {@inheritdoc}
     */
    public function sequence(int $currentMillisecond)
    {
          // Just test.
        return mt_rand(0, 1);
    }
}

// usage

$snowflake->setSequenceResolver(new YourSequence);
$snowflake->id();
```

And you also can use the Closure:

```
$snowflake = new \Godruoyi\Snowflake\Snowflake;
$snowflake->setSequenceResolver(function ($currentMillisecond) {
    static $lastTime;
    static $sequence;

    if ($lastTime == $currentMillisecond) {
        ++$sequence;
    } else {
        $sequence = 0;
    }

    $lastTime = $currentMillisecond;

    return $sequence;
})->id();
```

License
-------

[](#license)

MIT

###  Health Score

30

—

LowBetter than 65% of packages

Maintenance29

Infrequent updates — may be unmaintained

Popularity2

Limited adoption so far

Community12

Small or concentrated contributor base

Maturity68

Established project with proven stability

 Bus Factor1

Top contributor holds 92.6% 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 ~70 days

Recently: every ~106 days

Total

25

Last Release

762d ago

Major Versions

1.1.1 → 2.0.02021-12-12

2.2.4 → 3.0.02023-08-28

### Community

Maintainers

![](https://www.gravatar.com/avatar/3abde3fccf025ce86670101d7cfa0641d2415c9daad93e5f0c9573a2b23ddbda?d=identicon)[guanhui07](/maintainers/guanhui07)

---

Top Contributors

[![godruoyi](https://avatars.githubusercontent.com/u/16079222?v=4)](https://github.com/godruoyi "godruoyi (100 commits)")[![simivar](https://avatars.githubusercontent.com/u/828020?v=4)](https://github.com/simivar "simivar (2 commits)")[![cyz-home](https://avatars.githubusercontent.com/u/15663154?v=4)](https://github.com/cyz-home "cyz-home (2 commits)")[![WuHuaJi0](https://avatars.githubusercontent.com/u/9860748?v=4)](https://github.com/WuHuaJi0 "WuHuaJi0 (1 commits)")[![guanhui07](https://avatars.githubusercontent.com/u/5820457?v=4)](https://github.com/guanhui07 "guanhui07 (1 commits)")[![AaronChengHao](https://avatars.githubusercontent.com/u/20119217?v=4)](https://github.com/AaronChengHao "AaronChengHao (1 commits)")[![hernandev](https://avatars.githubusercontent.com/u/1143355?v=4)](https://github.com/hernandev "hernandev (1 commits)")

---

Tags

unique-idsnowflake algorithmphp snowflakelaravel snowflakeorder idunique order idphp unique idsonyflakephp sonyflake

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StyleLaravel Pint

Type Coverage Yes

### Embed Badge

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

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

###  Alternatives

[godruoyi/php-snowflake

An ID Generator for PHP based on Snowflake Algorithm (Twitter announced).

8582.3M61](/packages/godruoyi-php-snowflake)[pocketmine/pocketmine-mp

A server software for Minecraft: Bedrock Edition written in PHP

3.5k74.6k86](/packages/pocketmine-pocketmine-mp)[pocketmine/math

PHP library containing math related code used in PocketMine-MP

45573.2k14](/packages/pocketmine-math)[pocketmine/nbt

PHP library for working with Named Binary Tags

42574.4k13](/packages/pocketmine-nbt)[pocketmine/snooze

Thread notification management library for code using the pthreads extension

19565.1k8](/packages/pocketmine-snooze)[danog/telegram-entities

A library to work with Telegram UTF-16 styled text entities.

23203.1k5](/packages/danog-telegram-entities)

PHPackages © 2026

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