PHPackages                             c0dec0de/snowflake-id - 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. c0dec0de/snowflake-id

ActiveLibrary

c0dec0de/snowflake-id
=====================

A PHP library for generating Snowflake IDs

v0.1.3(9mo ago)228MITPHPCI passing

Since Jun 28Pushed 9mo agoCompare

[ Source](https://github.com/c0dec0de/snowflake-id)[ Packagist](https://packagist.org/packages/c0dec0de/snowflake-id)[ RSS](/packages/c0dec0de-snowflake-id/feed)WikiDiscussions 0.x Synced 1mo ago

READMEChangelog (4)Dependencies (10)Versions (5)Used By (0)

c0dec0de/snowflake-id
=====================

[](#c0dec0desnowflake-id)

 **A PHP library for generating Snowflake IDs**

[![GitHub release](https://camo.githubusercontent.com/442a65da2ceef59036f8c3b0c582eab1d0260004a1c7d06ea38a3faac14c5649/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f762f72656c656173652f63306465633064652f736e6f77666c616b652d6964)](https://github.com/c0dec0de/snowflake-id/releases)[![PHP](https://camo.githubusercontent.com/73ef36a03e88ffed2a93cdf66468720c551168579fa01918500e5388bded218a/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048502d253545382e342d626c7565)](https://camo.githubusercontent.com/73ef36a03e88ffed2a93cdf66468720c551168579fa01918500e5388bded218a/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048502d253545382e342d626c7565)[![Build](https://github.com/c0dec0de/snowflake-id/actions/workflows/ci.yml/badge.svg)](https://github.com/c0dec0de/snowflake-id/actions)[![Tested With PHPUnit](https://camo.githubusercontent.com/fb9f02831b9586bb3f8741cb600a5b9c4796c604546ca8dd0ffd5fe722f9a376/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5465737465645f776974682d504850556e69742d626c7565)](https://camo.githubusercontent.com/fb9f02831b9586bb3f8741cb600a5b9c4796c604546ca8dd0ffd5fe722f9a376/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5465737465645f776974682d504850556e69742d626c7565)[![Coverage Status](https://camo.githubusercontent.com/8bbf9bdf7954576035c1825d4f69949daaa699e17dad505867e90d9a4064dc4d/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f63306465633064652f736e6f77666c616b652d69642f62616467652e7376673f6272616e63683d302e78)](https://coveralls.io/github/c0dec0de/snowflake-id?branch=0.x)[![PHPStan](https://camo.githubusercontent.com/53ba6edb4eb129fec3f0da188729e7cb234cdc0d49f3405c6fee07ddc2540db0/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048505374616e2d4c6576656c253230392d627269676874677265656e3f6c6f676f3d706870)](https://camo.githubusercontent.com/53ba6edb4eb129fec3f0da188729e7cb234cdc0d49f3405c6fee07ddc2540db0/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048505374616e2d4c6576656c253230392d627269676874677265656e3f6c6f676f3d706870)[![Psalm Level 1](https://camo.githubusercontent.com/94da634f843d8d98e289a95692eb03bfed464150bafb9a5a8e81037b6d68ab1d/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5073616c6d2d4c6576656c253230312d626c756576696f6c65743f6c6f676f3d706870)](https://camo.githubusercontent.com/94da634f843d8d98e289a95692eb03bfed464150bafb9a5a8e81037b6d68ab1d/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5073616c6d2d4c6576656c253230312d626c756576696f6c65743f6c6f676f3d706870)[![License](https://camo.githubusercontent.com/3e5f1edcab0835ffa8bcc8932692aa21eaeef9faa820be59c1d7b978d6a6e763/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f63306465633064652f736e6f77666c616b652d6964)](https://camo.githubusercontent.com/3e5f1edcab0835ffa8bcc8932692aa21eaeef9faa820be59c1d7b978d6a6e763/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f63306465633064652f736e6f77666c616b652d6964)

### 📦 Installation

[](#-installation)

The recommended way to install the package is via [Composer](https://getcomposer.org):

```
composer require c0dec0de/snowflake-id
```

❄️ What is a Snowflake ID?
--------------------------

[](#️-what-is-a-snowflake-id)

A **Snowflake ID** is a unique 64-bit integer used in distributed systems to generate sortable, collision-resistant identifiers without a central source. Originally developed by **Twitter**, the format has been adopted by platforms like **Discord**, **Instagram**, and **Mastodon**.

Each Snowflake originally consists of:

- **41 bits** for a timestamp (in milliseconds since a custom epoch)
- **10 bits** for a machine ID
- **12 bits** for a sequence number (allowing multiple IDs per millisecond) This structure ensures that IDs are **unique**, **time-sortable**, and can be generated at high throughput across distributed nodes.

This library follows the original Snowflake structure closely but splits the 10 bits originally allocated for the machine ID into two parts, using the following layout:

- **41 bits** for a timestamp (milliseconds since a custom epoch)
- **5 bits** for a datacenter ID
- **5 bits** for a worker (machine) ID
- **12 bits** for a sequence number (allowing multiple IDs per millisecond)

This split provides finer control over deployment in multi-datacenter environments.

Benefits of Snowflakes
----------------------

[](#benefits-of-snowflakes)

- Distributed-safe 64bit unique human-readable ID generation
- Time sortable by design
- 64-bit integer output: smaller index, faster joins compared to 128-bit alternatives such a UUIDs or ULIDs

**Usage**
=========

[](#usage)

🧙 Let's play with Snowflakes
----------------------------

[](#-lets-play-with-snowflakes)

You can start by generating your first Snowflake ID by running this code:

```
