PHP code example of ramsey / identifier

1. Go to this page and download the library: Download ramsey/identifier library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

ramsey / identifier example snippets


use Ramsey\Identifier\Snowflake\GenericSnowflakeFactory;
use Ramsey\Identifier\Snowflake\Epoch;
use Ramsey\Identifier\Ulid\UlidFactory;
use Ramsey\Identifier\Uuid\UuidFactory;

// Create a UUID.
$uuid = (new UuidFactory())->create();

// Create a ULID.
$ulid = (new UlidFactory())->create();

// Create a Snowflake.
$snowflake = (new GenericSnowflakeFactory(1, Epoch::Unix))->create();

use Ramsey\Identifier\Uuid\NamespaceId;

$factory = new UuidFactory();

// Create random UUIDs (version 4).
$uuidV4 = $factory->v4();

// Create named-based UUIDs using SHA-1 hashing (version 5).
$uuidV5 = $factory->v5(NamespaceId::Url, 'https://example.com/post/1234');

// Create Unix Epoch time-based UUIDs (version 7).
$uuidV7 = $factory->v7();

// Parse a UUID.
$uuid = (new UuidFactory())->createFromString('01977bea-d1c0-7154-87bb-6550974155c2');

// Parse a ULID.
$ulid = (new UlidFactory())->createFromString('01JXXYNME0E5A8FEV5A2BM2NE2');

// Parse a Snowflake ID.
$snowflake = (new GenericSnowflakeFactory(1, Epoch::Unix))->createFromInteger(7340580095540599922);

echo $uuid->getDateTime()->format('Y-m-d H:i:s.v P') . "\n";
echo $ulid->getDateTime()->format('Y-m-d H:i:s.v P') . "\n";
echo $snowflake->getDateTime()->format('Y-m-d H:i:s.v P') . "\n";

if ($uuid->equals($ulid)) {
    echo "They are equal!\n";
}

if ($uuid->equals($snowflake)) {
    echo "The timestamp is the same, but they aren't equal.\n"
}