UUID <=> ULID bidirectional converter

Overview

uuid-ulid-converter

UUID <=> ULID bidirectional converter

Installing

composer require mpyw/uuid-ulid-converter

API

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

NOTE: UUID is lowercase by default, whereas ULID is uppercase by default.

Usage

Basic

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"

Advanced

The following workarounds are particularly useful in the PostgreSQL, which does not support ULID but does support UUID. Storing ULID as native UUID is more efficient than storing original ULID as strings.

Introduce advanced usages with robinvdvleuten/php-ulid.

Generate UUID-styled ULID

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

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

Timestamp range of UUID-styled ULID

string(36) "016f638d-4400-ffff-ffff-ffffffffffff" } */ ">
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"
}
*/
You might also like...
PHP Class converter to namepaces.

Namespacer This tool will assist you in taking older underscore/prefix spaced code and will namespace it as best as it can, and also make the files an

Arbitrary number base converter.

Numbase Easily convert numbers between arbitrary bases and symbol sets. Installation This library is available on Packagist as thunderer/numbase. It r

HTML to PDF converter for PHP

Dompdf Dompdf is an HTML to PDF converter At its heart, dompdf is (mostly) a CSS 2.1 compliant HTML layout and rendering engine written in PHP. It is

An ANSI to HTML5 converter

ANSI to HTML5 Converter This small library only does one thing: converting a text containing ANSI codes to an HTML5 fragment: require_once __DIR__.'/v

Laravel: Data to Monthly Converter

Laravel: Data to Monthly Converter Using this package you will be able to converts your collection using a timestamp field to order data monthly or ye

Unit converter and calculator for php

Unit converter and calculator This library uses the awesome lisachenko/z-engine to allow mathematical operations on objects, allowing to do stuff like

Google-like values converter

Google-like values converter. Support for different types of conversions, for examples: 1 kilometer - meters 1 dollar - THB 1 kilogram - meters ...

PHPUnit to Pest Converter

PestConverter PestConverter is a PHP library for converting PHPUnit tests to Pest tests. Before use Before using this converter, make sure your files

A full-featured home hosted Cloud Drive, Personal Assistant, App Launcher, File Converter, Streamer, Share Tool & More!
A full-featured home hosted Cloud Drive, Personal Assistant, App Launcher, File Converter, Streamer, Share Tool & More!

A Fully Featured home-hosted Cloud Storage platform and Personal Assistant that Converts files, OCR's images & documents, Creates archives, Scans for viruses, Protects your server, Keeps itself up-to-date, and Runs your own AppLauncher!

JavaScript to JQuery Converter

JQuery To Javascript Converter for helping to removing JQuery on websites.

The WordPress Categories to Tags Converter.

=== Categories to Tags Converter === Contributors: wordpressdotorg Donate link: Tags: importer, categories and tags converter Requires at least: 3.0

Releases(v1.0.0)
Owner
mpyw
mpyw
JavaScript to JQuery Converter

JQuery To Javascript Converter for helping to removing JQuery on websites.

Michael Parisi 13 Nov 21, 2022
The WordPress Categories to Tags Converter.

=== Categories to Tags Converter === Contributors: wordpressdotorg Donate link: Tags: importer, categories and tags converter Requires at least: 3.0

WordPress 3 Dec 6, 2022
Use UUID or Ulid as optional or primary key in Laravel.

Laravel OptiKey Use UUID or Ulid as optional or primary key in Laravel. composer require riipandi/laravel-optikey This package adds a very simple trai

Aris Ripandi 33 Nov 4, 2022
Laravel package to generate and to validate a UUID according to the RFC 4122 standard. Only support for version 1, 3, 4 and 5 UUID are built-in.

Laravel Uuid Laravel package to generate and to validate a universally unique identifier (UUID) according to the RFC 4122 standard. Support for versio

Christoph Kempen 1.7k Dec 28, 2022
This package lets you add uuid as primary key in your laravel applications

laravel-model-uuid A Laravel package to add uuid to models Table of contents Installation Configuration Model Uuid Publishing files / configurations I

salman zafar 10 May 17, 2022
Use auto generated UUID slugs to identify and retrieve your Eloquent models.

Laravel Eloquent UUID slug Summary About Features Requirements Installation Examples Compatibility table Alternatives Tests About By default, when get

Khalyomede 25 Dec 14, 2022
A laravel package to attach uuid to model classes

Laravel Model UUID A simple package to generate model uuid for laravel models Installation Require the package using composer: composer require touhid

null 10 Jan 20, 2022
This package provides a trait that will generate a unique uuid when saving any Eloquent model.

Generate slugs when saving Eloquent models This package provides a trait that will generate a unique uuid when saving any Eloquent model. $model = new

Abdul Kudus 2 Oct 14, 2021
A simple drop-in solution for providing UUID support for the IDs of your Eloquent models.

Introduction A simple drop-in solution for providing UUID support for the IDs of your Eloquent models. Both v1 and v4 IDs are supported out of the box

GoldSpec Digital 501 Jan 4, 2023
Generate UUID for a Laravel Eloquent model attribute

Generate a UUIDv4 for the primary key or any other attribute on an Eloquent model.

Alex Bouma 4 Mar 1, 2022