PHP code example of mpyw / uuid-ulid-converter

1. Go to this page and download the library: Download mpyw/uuid-ulid-converter 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/ */

    

mpyw / uuid-ulid-converter example snippets


public static Converter::uuidToUlid(string $uuid, bool $lowercase = false): string
public static Converter::ulidToUuid(string $ulid, bool $uppercase = false): string

use Mpyw\UuidUlidConverter\Converter;

var_dump(Converter::ulidToUuid('61862H2EWP9TCTRX3MJ15XNY7X'));
// string(36) "c1418511-3b96-4e99-ac74-74904bdaf8fd"

var_dump(Converter::uuidToUlid('c1418511-3b96-4e99-ac74-74904bdaf8fd'));
// string(26) "61862H2EWP9TCTRX3MJ15XNY7X"

use Mpyw\UuidUlidConverter\Converter;
use Ulid\Ulid;

// Use generated UUID as primary key
$uuid = Converter::ulidToUuid((string)Ulid::generate());

use Mpyw\UuidUlidConverter\Converter;
use Ulid\Ulid;

$dates = [
    new \DateTimeImmutable('2020-01-01 00:00:00.000 UTC'),
    new \DateTimeImmutable('2020-01-02 00:00:00.000 UTC'),
];

function createUuidRange(array $dates): array
{
    $createPart = fn (\DateTimeInterface $date, bool $isEnd) => Converter::ulidToUuid(
        Ulid::fromTimestamp(round((int)$date->format('Uu') / 1000))->getTime()
        . str_repeat($isEnd ? 'Z' : '0', 16),
    );
    return [
        $createPart($dates[0], false),
        $createPart($dates[1], true),
    ];
}

$uuids = createUuidRange($dates);
/*
array(2) {
  [0]=>
  string(36) "016f5e66-e800-0000-0000-000000000000"
  [1]=>
  string(36) "016f638d-4400-ffff-ffff-ffffffffffff"
}
*/