Search by

phpgt / ulid

g105b

Unique, lexicographically sortable identifiers.

Package info

github.com/PhpGt/Ulid

Issues

pkg:composer/phpgt/ulid

Fund package maintenance!

PhpGt

Statistics

Installs: 8 578

Dependents: 3

Suggesters: 0

Stars: 0

v1.2.3 2026-08-25 12:01 UTC

This package is auto-updated.

Last update: 2026-11-29 18:35:21 UTC


README

Create a new Ulid() anywhere in your application, and you have a stringable object that can be used as the primary key in a database. Ulid strings look something like 01G2J6MYN0PGC5Q21W9C or can be prefixed with a type like CUSTOMER_01G2J6MYN0PGC5Q21W9C. They are cryptographically pseudo-random, and sort so that newer Ulids compare "greater than" older Ulids.

This solves the problems exposed with working with auto-incrementing integer primary keys, which are predictable and difficult to work with in distributed databases.

Build status Code quality Code coverage Current version PHP.Gt/Ulid documentation

Example usage:

use GT\Ulid\Ulid;

$exampleDataSource->create(new Person(
	new Ulid("pet"),
	name: "Cody",
	age: 5,
));

Explicit timestamps

$ulid = new Ulid('pet', timestampSeconds: 1712345678.123);
$seconds = $ulid->getTimestampSeconds(); // 1712345678.123
$milliseconds = $ulid->getTimestampMilliseconds(); // 1712345678123

timestampSeconds accepts integer or fractional Unix seconds and rounds to the nearest millisecond. Supply it on its own when choosing an explicit timestamp.

The second constructor argument, timestamp, still accepts Unix milliseconds but is deprecated. To migrate, use timestampSeconds: $milliseconds / 1000. Replace calls to the deprecated getTimestamp() with getTimestampMilliseconds() to keep the same integer value.

Proudly sponsored by

JetBrains Open Source sponsorship program

JetBrains logo.