All methods belong to Webpatser\Ulid\Ulid. The constructor is protected; create instances through the static methods below. The factory and query methods carry #[\NoDiscard].
Generation#
| Method |
Description |
Ulid::generate(?DateTimeInterface $time = null) |
Generate a monotonic ULID, optionally with a specific timestamp |
Ulid::fromString(string $ulid) |
Parse a 26-char Crockford Base32 string |
Ulid::fromBytes(string $bytes) |
Create from 16-byte binary |
Ulid::fromUuid(Uuid $uuid) |
Create from a Webpatser\Uuid\Uuid |
Ulid::isValid(string $ulid) |
Validate ULID format, returns bool |
Instance methods#
| Method |
Description |
(string) $ulid |
26-char Crockford Base32 string |
$ulid->toBytes() |
16-byte binary representation |
$ulid->getTimestamp() |
DateTimeInterface from the embedded timestamp |
$ulid->getTimestampMs() |
Raw millisecond timestamp (int) |
$ulid->toUuid() |
Convert to Webpatser\Uuid\Uuid (same 128 bits) |
Errors#
| Call |
Throws |
fromString() with an invalid string |
Exception with Invalid ULID string. |
fromBytes() with a string that is not 16 bytes |
Exception with Input must be a 128-bit (16-byte) binary string. |
generate() when the monotonic counter overflows within one millisecond |
RuntimeException with ULID monotonic counter overflow |
Validation rules#
isValid() returns false unless the string is exactly 26 characters, every character is in the Crockford Base32 alphabet (case insensitive), and the first character is 0 to 7. The first character holds the top 3 bits of the 128-bit value, so larger values would overflow the 48-bit timestamp.