PHPackages                             tuupola/ksuid - 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. tuupola/ksuid

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

tuupola/ksuid
=============

K-Sortable Globally Unique IDs

2.1.1(2mo ago)1081.2M↓44.1%3[1 issues](https://github.com/tuupola/ksuid/issues)[1 PRs](https://github.com/tuupola/ksuid/pulls)4MITPHPPHP ^7.1|^8.0CI passing

Since Jun 9Pushed 2mo ago4 watchersCompare

[ Source](https://github.com/tuupola/ksuid)[ Packagist](https://packagist.org/packages/tuupola/ksuid)[ Docs](https://github.com/tuupola/ksuid)[ RSS](/packages/tuupola-ksuid/feed)WikiDiscussions 2.x Synced 1mo ago

READMEChangelogDependencies (10)Versions (17)Used By (4)

KSUID
=====

[](#ksuid)

This library implements the [K-Sortable Globally Unique IDs](https://github.com/segmentio/ksuid) from Segment. See also the article called [A Brief History of the UUID](https://segment.com/blog/a-brief-history-of-the-uuid/).

> KSUID is for K-Sortable Unique IDentifier. It's a way to generate globally unique IDs similar to RFC 4122 UUIDs, but contain a time component so they can be "roughly" sorted by time of creation. The remainder of the KSUID is randomly generated bytes.

[![Latest Version](https://camo.githubusercontent.com/0764a6a8c8e506eafda1b496032748d934f66e2490688199e4c17d41443775cf/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f747575706f6c612f6b737569642e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/tuupola/ksuid)[![Downloads](https://camo.githubusercontent.com/d52d72e10518db77a18cfe333d3ca766d345a0b4ac3c2d27a377ba2ec0091c52/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f646d2f747575706f6c612f6b737569642e737667)](https://packagist.org/packages/tuupola/ksuid/stats)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](https://github.com/tuupola/ksuid/blob/2.x/LICENSE)[![Build Status](https://camo.githubusercontent.com/50afcb7db68a73457e0613593e9eb488eeaf047cf6cbdb9265181bf0f3c4be47/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f747575706f6c612f6b737569642f74657374732e796d6c3f6272616e63683d322e78267374796c653d666c61742d737175617265)](https://github.com/tuupola/ksuid/actions)[![Coverage](https://camo.githubusercontent.com/fefe30aa1e7b29b42d4d0e21088b449e20ca981a8a2b8e42cae464d316bbba54/68747470733a2f2f696d672e736869656c64732e696f2f636f6465636f762f632f6769746875622f747575706f6c612f6b737569642e7376673f7374796c653d666c61742d737175617265)](https://codecov.io/github/tuupola/ksuid)

Install
-------

[](#install)

Install with [composer](https://getcomposer.org/).

```
$ composer require tuupola/ksuid
```

This branch requires PHP 7.1 or up. The older `1.x` branch supports also PHP 5.6 and 7.0.

```
$ composer require "tuupola/ksuid:^1.0"
```

Usage
-----

[](#usage)

Included Base62 implementation has both PHP and [GMP](http://php.net/manual/en/ref.gmp.php) based encoders. By default encoder and decoder will use GMP functions if the extension is installed. If GMP is not available pure PHP encoder will be used instead.

Note! Throughout the code the term `timestamp` refers to KSUID timestamp. The term `unixtime` refers to the traditional Unix time. KSUID timestamp and Unix time have different Epoch.

```
use Tuupola\Ksuid;

$ksuid = new Ksuid;

print $ksuid; /* p6UEyCc8D8ecLijAI5zVwOTP3D0 */

print $ksuid->timestamp(); /* 94985761 */
print $ksuid->unixtime(); /* 1494985761 */
print bin2hex($ksuid->payload()); /* d7b6fe8cd7cff211704d8e7b9421210b */

$datetime = (new \DateTimeImmutable)
    ->setTimestamp($ksuid->unixtime())
    ->setTimeZone(new \DateTimeZone("UTC"));

print $datetime->format("Y-m-d H:i:s"); /* 2017-05-17 01:49:21 */
```

If you prefer static syntax you can use one of the provided factories.

```
use Tuupola\KsuidFactory as Ksuid;

$ksuid = Ksuid::create();

$ksuid = Ksuid::fromString("0o5Fs0EELR0fUjHjbCnEtdUwQe3");

$binary = hex2bin("05a95e21d7b6fe8cd7cff211704d8e7b9421210b");
$ksuid = Ksuid::fromBytes($binary);

$ksuid = Ksuid::fromTimestamp(94985761);

$ksuid = Ksuid::fromUnixtime(1494985761);

$timestamp = 94985761;
$payload = hex2bin("d7b6fe8cd7cff211704d8e7b9421210b");
$ksuid = Ksuid::fromTimestampAndPayload($timestamp, $payload);
```

Testing
-------

[](#testing)

You can run tests either manually or automatically on every code change. Automatic tests require [entr](http://entrproject.org/) to work.

```
$ make test
```

```
$ brew install entr
$ make watch
```

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](CONTRIBUTING.md) for details.

Security
--------

[](#security)

If you discover any security related issues, please email  instead of using the issue tracker.

License
-------

[](#license)

The MIT License (MIT). Please see [License File](https://github.com/tuupola/ksuid/blob/2.x/LICENSE) for more information.

###  Health Score

63

—

FairBetter than 99% of packages

Maintenance87

Actively maintained with recent releases

Popularity53

Moderate usage in the ecosystem

Community20

Small or concentrated contributor base

Maturity76

Established project with proven stability

 Bus Factor1

Top contributor holds 97.9% 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 ~228 days

Recently: every ~503 days

Total

15

Last Release

63d ago

Major Versions

0.3.0 → 1.0.02019-01-05

1.0.1 → 2.0.02019-01-06

1.0.2 → 2.1.12026-03-16

PHP version history (4 changes)0.1.0PHP ^5.6 || ^7.0

0.1.3PHP ^5.6|^7.0

2.0.0PHP ^7.1

2.1.0PHP ^7.1|^8.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/3325405a7d8a43bc40dd0e760a4b7f268fba32a7150cf0327f64f13d1661df0b?d=identicon)[tuupola](/maintainers/tuupola)

---

Top Contributors

[![tuupola](https://avatars.githubusercontent.com/u/21913?v=4)](https://github.com/tuupola "tuupola (95 commits)")[![peter279k](https://avatars.githubusercontent.com/u/9021747?v=4)](https://github.com/peter279k "peter279k (1 commits)")[![rodrigopedra](https://avatars.githubusercontent.com/u/5470108?v=4)](https://github.com/rodrigopedra "rodrigopedra (1 commits)")

---

Tags

base62

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP\_CodeSniffer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/tuupola-ksuid/health.svg)

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

###  Alternatives

[elfsundae/laravel-hashid

A simple, elegant way to obfuscate your data by generating reversible, non-sequential, URL-safe identifiers.

415246.3k2](/packages/elfsundae-laravel-hashid)[tuupola/base62

Base62 encoder and decoder for arbitrary data

1994.3M35](/packages/tuupola-base62)[xobotyi/basen

Text and integers encoding utilities for PHP with no extensions dependencies. Base32, Base58, Base64 and much more!

1219.6k](/packages/xobotyi-basen)[base62/base62

base62 encoder and decoder also for big numbers with Laravel integration

169.5k](/packages/base62-base62)

PHPackages © 2026

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