~/docs
Resonate
Resonate
4.x 4.0.0, current 3.x 3.0.1 2.x 2.0.1 1.x 1.3.0
UUID Library
Toggle sidebar
UUID Library
webpatser/uuid 3.0.1
You are reading the 3.x documentation
The current line is 4.x. Read this page for 4.x

UUID Library 3.x

Examples

Complete, copyable examples for generating, importing, comparing and inspecting UUIDs with webpatser/uuid, plus a live validator.

The live validator below uses the same Uuid::validate() and Uuid::import() calls as the code on this page.

⚡ Quick Actions

This interactive experience is powered by Laravel, Livewire & PHP, using the actual UUID library for real-time generation and validation

🎲 Interactive UUID Generator

19337b86-0bf9-4832-9dba-7c74d3aa625f
19337b860bf948329dba7c74d3aa625f
19337b860bf948329dba7c74d3aa625f

🔍 UUID Validator & Inspector

Enter a UUID above to validate

The scripts below follow examples/basic-usage.php and examples/time-based.php in the repository. Run them from the package root after composer install.

Generate every version

<?php
 
require_once __DIR__ . '/../vendor/autoload.php';
 
use Webpatser\Uuid\Uuid;
 
// Random
$uuid4 = Uuid::v4();
echo "UUID v4: " . (string) $uuid4 . "\n";
 
// Time-ordered, recommended for databases
$uuid7 = Uuid::v7();
echo "UUID v7: " . (string) $uuid7 . "\n";
 
// Other versions
$uuid1 = Uuid::generate(1);
echo "UUID v1: " . (string) $uuid1 . "\n";
 
$uuid3 = Uuid::generate(3, 'example.com', Uuid::NS_DNS);
echo "UUID v3: " . (string) $uuid3 . "\n";
 
$uuid5 = Uuid::generate(5, 'example.com', Uuid::NS_DNS);
echo "UUID v5: " . (string) $uuid5 . "\n";
 
$uuid6 = Uuid::generate(6);
echo "UUID v6: " . (string) $uuid6 . "\n";
 
$uuid8 = Uuid::generate(8);
echo "UUID v8: " . (string) $uuid8 . "\n";

The v3 and v5 lines always print 9073926b-929f-31c2-abc9-fad77ae3e8eb and cfbff0d1-9375-5685-968c-48ce8b15ae17.

Live output

The block below generates one UUID per version on this server, each next to the call that produced it. Press Generate again for new values. The name-based rows are always the same for the same name and namespace.

Generated on this server for this page view
  • Uuid::v4() v4, random
    6fadb6e2-36f7-4a35-99f3-5be931a5c16d
  • Uuid::v7() v7, time-ordered
    01a23843-36f9-7009-90bd-afd6d4b00210
  • Uuid::generate(1) v1, time and node
    d514c100-ee87-11f1-9c3c-c9a4d00520ae
  • Uuid::generate(3, 'example.com', Uuid::NS_DNS) v3, MD5 name-based
    9073926b-929f-31c2-abc9-fad77ae3e8eb
  • Uuid::generate(5, 'example.com', Uuid::NS_DNS) v5, SHA-1 name-based
    cfbff0d1-9375-5685-968c-48ce8b15ae17
  • Uuid::generate(6) v6, time-ordered (RFC 9562)
    1f1ee87d-514c-6160-b5aa-65a259641b0e
  • Uuid::generate(8) v8, custom
    f78bd7f5-1ccb-8cc0-8928-87fc4dc5e782
  • Uuid::nil() Nil
    00000000-0000-0000-0000-000000000000

The v3 and v5 values stay the same on every view; the others change each time.

Import, validate and inspect

<?php
 
require_once __DIR__ . '/../vendor/autoload.php';
 
use Webpatser\Uuid\Uuid;
 
// Import an existing UUID string
$imported = Uuid::import('6ba7b810-9dad-11d1-80b4-00c04fd430c8');
echo "Imported: " . (string) $imported . "\n";
echo "Version: " . $imported->version . "\n";
echo "Variant: " . $imported->variant . "\n";
 
// Validate UUID strings
$valid = '6ba7b810-9dad-11d1-80b4-00c04fd430c8';
echo "Is valid UUID: " . (Uuid::validate($valid) ? 'yes' : 'no') . "\n";
 
$invalid = 'not-a-uuid';
echo "Is valid UUID: " . (Uuid::validate($invalid) ? 'yes' : 'no') . "\n";
 
// Properties of a v1 UUID
$uuid = Uuid::generate(1);
echo "String: " . (string) $uuid . "\n";
echo "Hex: " . $uuid->hex . "\n";
echo "URN: " . $uuid->urn . "\n";
echo "Node: " . $uuid->node . "\n";
 
// Nil UUID
$nil = Uuid::nil();
echo "Nil UUID: " . (string) $nil . "\n";
echo "Is nil: " . ($nil->isNil() ? 'yes' : 'no') . "\n";

Time-ordered UUIDs and time extraction

<?php
 
require_once __DIR__ . '/../vendor/autoload.php';
 
use Webpatser\Uuid\Uuid;
 
// Five v7 UUIDs, one millisecond apart
for ($i = 1; $i <= 5; $i++) {
$uuid = Uuid::v7();
echo "UUID #{$i}: " . (string) $uuid . "\n";
 
usleep(1000); // 1ms
}
 
// Comparison
$uuid1 = Uuid::v7();
$uuid2 = Uuid::v7();
 
echo "Are equal: " . (Uuid::compare((string) $uuid1, (string) $uuid2) ? 'yes' : 'no') . "\n";
echo "UUID 1 == UUID 1: " . (Uuid::compare((string) $uuid1, (string) $uuid1) ? 'yes' : 'no') . "\n";
 
// Time extraction (v1, v6 and v7)
$v1 = Uuid::generate(1);
echo "V1 Time: " . $v1->time . "\n";
 
$v6 = Uuid::generate(6);
echo "V6 Time: " . $v6->time . "\n";
 
$v7 = Uuid::v7();
echo "V7 Time: " . $v7->time . "\n";
echo "V7 DateTime: " . date('Y-m-d H:i:s', (int) $v7->time) . "\n";

Deterministic IDs under your own namespace

Derive a namespace UUID once, then use it for every name. The namespace argument is a string, so pass ->string.

use Webpatser\Uuid\Uuid;
 
$appNamespace = Uuid::generate(5, 'myapp.com', Uuid::NS_DNS)->string;
 
$userIds = [];
foreach (['alice', 'bob', 'charlie'] as $username) {
$userIds[$username] = Uuid::generate(5, $username, $appNamespace);
}

Next steps