Everything lives in one class, Webpatser\Ulid\Ulid.
use Webpatser\Ulid\Ulid; // Generate a ULID$ulid = Ulid::generate();echo (string) $ulid; // e.g., "01ARZ3NDEKTSV4RRFFQ69G5FAV" // Generate with a specific timestamp$ulid = Ulid::generate(new DateTimeImmutable('2024-01-15 12:00:00')); // Parse an existing ULID$ulid = Ulid::fromString('01ARZ3NDEKTSV4RRFFQ69G5FAV'); // ValidateUlid::isValid('01ARZ3NDEKTSV4RRFFQ69G5FAV'); // trueUlid::isValid('not-a-ulid'); // false
The format
- 26 characters, Crockford Base32 encoded (0-9, A-H, J-K, M-N, P-T, V-Z).
- A 48-bit millisecond timestamp (first 10 characters) followed by 80 bits of randomness (last 16 characters).
- Monotonic: multiple ULIDs within the same millisecond increment the random portion.
- Lexicographically sortable: string comparison preserves chronological order.
- Binary compatible: 16 bytes, the same size as a UUID, so it fits in
BINARY(16)columns. - Case insensitive when parsing:
fromString()andisValid()accept upper and lowercase.
Features
#[\NoDiscard]on all factory and query methods, so PHP warns when a result is ignored.Random\Randomizersupplies the random bytes.- The 16 bytes are held in a
readonlyproperty. - Bidirectional UUID conversion through
webpatser/uuid.
Try it
Generate a ULID or a batch of up to 20. Each one comes from Ulid::generate() on this server, and the batch shows that string order is creation order.
Ulid::generate(), called 1 time on this server just now, listed in creation order.
-
1
01M86YFXMTBV8M3YBRG1GVCYA7
Batches run on demand, at most 20 a minute per visitor.
Inspect a ULID
Paste a ULID to validate it and read its timestamp and UUID form, or paste a UUID to convert it with Ulid::fromUuid().
(string) $ulid- 01ARZ3NDEKTSV4RRFFQ69G5FAV
$ulid->getTimestamp()- 2016-07-30 23:54:10.259 UTC
$ulid->getTimestampMs()- 1469922850259
(string) $ulid->toUuid()- 01563e3a-b5d3-d676-4c61-efb99302bd5b
bin2hex($ulid->toBytes())- 01563e3ab5d3d6764c61efb99302bd5b
See Examples and the API reference.