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

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

chexj/php-snowflake
===================

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

1.0.15(5y ago)01.9kMITPHP

Since Aug 12Pushed 5y agoCompare

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

READMEChangelog (2)DependenciesVersions (19)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).

 [ ![quality score](https://camo.githubusercontent.com/58e20f3550e94375d8b12534bf0f32d4b26c51ac41fbf21efbcd6716ddd2241c/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f676f6472756f79692f7068702d736e6f77666c616b652f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572) ](https://scrutinizer-ci.com/g/godruoyi/php-snowflake/) [ ![godruoyi php-snowflake](https://camo.githubusercontent.com/5b8f12bde3fe64b2dd00f644fe8d85384183a0cc907129182530d3a2484add1f/68747470733a2f2f6769746875622e7374796c6563692e696f2f7265706f732f3230313933363031332f736869656c643f6272616e63683d6d6173746572) ](https://github.com/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) [ ![build passed](https://camo.githubusercontent.com/09a3e01bb776477360b46ce1fcbc2b718caa0b06476a6eb841eff07a8aff1b01/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f676f6472756f79692f7068702d736e6f77666c616b652f6261646765732f6275696c642e706e673f623d6d6173746572) ](https://scrutinizer-ci.com/g/godruoyi/php-snowflake/) [ ![Total Downloads](https://camo.githubusercontent.com/10fa4f9c82a194b25b72c00ae26bc39e965b131cdaffb634ab13ff0c83f30b24/68747470733a2f2f706f7365722e707567782e6f72672f676f6472756f79692f7068702d736e6f77666c616b652f646f776e6c6f616473) ](https://github.com/godruoyi/php-snowflake)

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

[](#description)

Snowflake 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 for generating unique ID numbers at high scale with some simple guarantees.

- The first bit is unused sign bit.
- The second part consists of a 41-bit timestamp (milliseconds) whose value is the offset of the current time relative to a certain time.
- The 5 bits of the third and fourth parts represent data center and worker, and max value is 2^5 -1 = 31.
- The last part consists of 12 bits, its means the length of the serial number generated per millisecond per working node, a maximum of 2^12 -1 = 4095 IDs can be generated in the same millisecond.
- In a distributed environment, five-bit datacenter and worker mean that can deploy 31 datacenters, and each datacenter can deploy up to 31 nodes.
- The binary length of 41 bits is at most 2^41 -1 millisecond = 69 years. So the snowflake algorithm can be used for up to 69 years, In order to maximize the use of the algorithm, you should specify a start time for it.

> You must know, The ID generated by the snowflake algorithm is not guaranteed to be unique. For example, when two different requests enter the same node of the same data center at the same time, and the sequence generated by the node is the same, the generated ID will be duplicated.

So if you want use the snowflake algorithm to generate unique ID, You must ensure: The sequence-number generated in the same millisecond of the same node is unique. Based on this, we created this package and integrated multiple sequence-number providers into it.

- RandomSequenceResolver (Random)
- RedisSequenceResolver (based on redis psetex and incrby)
- LaravelSequenceResolver (based on redis psetex and incrby)
- SwooleSequenceResolver (based on swoole\_lock)

> Each provider only needs to ensure that the serial number generated in the same millisecond is different. You can get a unique ID.

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

[](#requirement)

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

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

[](#installation)

```
$ composer require chexj/php-snowflake
```

Useage
------

[](#useage)

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);

$snowflake->id();
```

Advanced
--------

[](#advanced)

1. Used in Laravel.

Because the SDK is relatively simple, we don't provide an extension for Laravel. You can quickly integrate it into Laravel in the following way.

```
// 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 () {
            return (new Snowflake())
                ->setStartTimeStamp(strtotime('2019-10-10')*1000)
                ->setSequenceResolver(new LaravelSequenceResolver($this->app->get('cache')->store()));
        });
    }
}
```

2. Custom

You can customize the sequence-number resolver by implementing the Godruoyi\\Snowflake\\SequenceResolver interface.

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

// usage

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

And you can use closure:

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

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

    $lastTime = $currentTime;

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

License
-------

[](#license)

MIT

###  Health Score

31

—

LowBetter than 68% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity15

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity67

Established project with proven stability

 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 ~37 days

Recently: every ~31 days

Total

16

Last Release

1911d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/67717fc42dab3d1860e099ac9a7726c121dbadaa67b9a48be81f3c835410cba8?d=identicon)[chexj](/maintainers/chexj)

---

Top Contributors

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

---

Tags

unique-idsnowflake algorithmphp snowflakelaravel snowflakeorder idunique order idphp unique id

### Embed Badge

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

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

###  Alternatives

[godruoyi/php-snowflake

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

8582.3M61](/packages/godruoyi-php-snowflake)[pattern-lab/edition-drupal-standard

Standard Edition of Pattern Lab for Drupal.

12419.1k](/packages/pattern-lab-edition-drupal-standard)

PHPackages © 2026

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