PHPackages                             guifelix/ulid - 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. guifelix/ulid

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

guifelix/ulid
=============

A PHP package to generate Universally Unique Lexicographically Sortable Identifiers

v0.1.0(4y ago)16[3 PRs](https://github.com/guifelix/ulid/pulls)MITPHPPHP ^8.0CI passing

Since Apr 4Pushed 4mo ago1 watchersCompare

[ Source](https://github.com/guifelix/ulid)[ Packagist](https://packagist.org/packages/guifelix/ulid)[ Docs](https://github.com/guifelix/ulid)[ GitHub Sponsors](https://github.com/guifelix)[ RSS](/packages/guifelix-ulid/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (1)Dependencies (8)Versions (5)Used By (0)

This is a PHP [ULID](https://github.com/ulid/spec) package
==========================================================

[](#this-is-a-php-ulid-package)

[![Latest Version on Packagist](https://camo.githubusercontent.com/0ef284809810b31e241948994147ba1989bb32b527278a194fd6655a5da98b30/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f67756966656c69782f756c69642e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/guifelix/ulid)[![Tests](https://github.com/guifelix/ulid/actions/workflows/run-tests.yml/badge.svg?branch=main)](https://github.com/guifelix/ulid/actions/workflows/run-tests.yml)[![Total Downloads](https://camo.githubusercontent.com/8468af1f7ca3ce99d818120674f7a1d9cea8429e0faba340a66a5dac4e457a2f/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f67756966656c69782f756c69642e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/guifelix/ulid)

PHP Library to use ULID on your application.

> - 128-bit compatibility with UUID
> - 1.21e+24 unique ULIDs per millisecond
> - Lexicographically sortable!
> - Canonically encoded as a 26 character string, as opposed to the 36 character UUID
> - [Uses Crockford's base32 for better efficiency and readability (5 bits per character)](https://github.com/ulid/spec#encoding)
> - Case insensitive
> - No special characters (URL safe)
> - Monotonic sort order (correctly detects and handles the same millisecond)

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

[](#installation)

You can install the package via composer:

```
composer require guifelix/php-ulid
```

Usage
-----

[](#usage)

### Generate

[](#generate)

```
use Guifelix\Ulid;

$ulid = Ulid::generate(); // Accept boolean as a parameter for lowercase;

echo (string) $ulid; //0001EH8YAEP8CXP4AMWCHHDBHJ
echo $ulid->getTime(); //0001EH8YAE
echo $ulid->getRandomness(); //P8CXP4AMWCHHDBHJ
echo $ulid->isLowercase(); //false
echo $ulid->toTimestamp(); //1561622862
```

### Generate from timestamp

[](#generate-from-timestamp)

```
use Guifelix\Ulid;

$ulid = Ulid::fromTimestamp(1561622862); // Accept boolean as a second parameter for lowercase;

echo (string) $ulid; //0001EH8YAEP8CXP4AMWCHHDBHJ
```

### Generate from string (doesn't increment randomness)

[](#generate-from-string-doesnt-increment-randomness)

```
use Guifelix\Ulid;

$ulid = Ulid::fromString('0001EH8YAEP8CXP4AMWCHHDBHJ'); // Accept boolean as a second parameter for lowercase;

echo (string) $ulid; //0001EH8YAEP8CXP4AMWCHHDBHJ
```

### Validate

[](#validate)

```
use Guifelix\Ulid;

Ulid::validate('8ZZZZZZZZZP8CXP4AMWCHHDBHI'); // Case insensitve
/**
 * validate Length, Crockford Characters and Time
 * Throws
 *  - InvalidUlidLengthException
 *  - InvalidUlidCharException
 *  - InvalidUlidTimestampException
