PHP code example of guifelix / ulid

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

    

guifelix / ulid example snippets


use Guifelix\Ulid;

$ulid = Ulid::generate(); // Accept boolean as a parameter for lowercase;

echo (string) $ulid; //0001EH8YAEP8CXP4AMWCHHDBHJ
echo $ulid->getTime(); //0001EH8YAE
echo $ulid->getRandomness(); //P8CXP4AMWCHHDBHJ
echo $ulid->isLowercase(); //false
echo $ulid->toTimestamp(); //1561622862

use Guifelix\Ulid;

$ulid = Ulid::fromTimestamp(1561622862); // Accept boolean as a second parameter for lowercase;

echo (string) $ulid; //0001EH8YAEP8CXP4AMWCHHDBHJ

use Guifelix\Ulid;

$ulid = Ulid::fromString('0001EH8YAEP8CXP4AMWCHHDBHJ'); // Accept boolean as a second parameter for lowercase;

echo (string) $ulid; //0001EH8YAEP8CXP4AMWCHHDBHJ

use Guifelix\Ulid;

Ulid::validate('8ZZZZZZZZZP8CXP4AMWCHHDBHI'); // Case insensitve
/**
 * validate Length, Crockford Characters and Time
 * Throws
 *  - InvalidUlidLengthException
 *  - InvalidUlidCharException
 *  - InvalidUlidTimestampException <- Max timestamp is 7ZZZZZZZZZ (281474976710655) or until the year 10889 AD :)
 *  - InvalidUlidException
 * /
bash
composer