-
-
Notifications
You must be signed in to change notification settings - Fork 0
Examples
Greg Bowler edited this page Apr 19, 2026
·
1 revision
This page gathers a few complete examples that mirror the most common ways we use phpgt/ulid in an application.
use GT\Ulid\Ulid;
$userId = new Ulid("user");
$orderId = new Ulid("order");
echo $userId, PHP_EOL;
echo $orderId, PHP_EOL;use GT\Ulid\Ulid;
$id = new Ulid("customer");
$db->insert("customer/insert", [
"id" => (string)$id,
"name" => "A. Example",
]);Because the object is stringable, it fits naturally into insert payloads.
use GT\Ulid\Ulid;
$storedId = $row->getString("id");
$id = new Ulid(init: $storedId);
echo $id->getPrefix(), PHP_EOL;
echo $id->getDateTime()->format("Y-m-d H:i:s"), PHP_EOL;$ids = [
(string)new Ulid(timestamp: strtotime("2024-03-01 10:00:00 UTC") * 1000),
(string)new Ulid(timestamp: strtotime("2024-01-01 10:00:00 UTC") * 1000),
(string)new Ulid(timestamp: strtotime("2024-02-01 10:00:00 UTC") * 1000),
];
sort($ids, SORT_STRING);
print_r($ids);Because the timestamp comes first, plain string sorting gives us chronological order.
use GT\Ulid\Extractor;
$id = "INVOICE_01KPKT4PFTKVQ9P1E72F";
$extractor = new Extractor();
printf("Prefix: %s\n", $extractor->extractPrefix($id));
printf("Timestamp: %d\n", $extractor->extractTimestamp($id));
printf("Random: %s\n", $extractor->extractRandomString($id));PHP.GT/Ulid is a separately maintained component of PHP.GT/WebEngine.