odan / tsid
A library for generating Time Sortable Identifiers (TSID).
Installs: 51 104
Dependents: 0
Suggesters: 0
Security: 0
Stars: 16
Watchers: 2
Forks: 4
Open Issues: 0
Requires
- php: ^8
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3
- phpstan/phpstan: ^1
- phpunit/phpunit: ^9 || ^10
- squizlabs/php_codesniffer: ^3
README
Description
A library for generating Time Sortable Identifiers (TSID).
This library is a port of TSID Creator from Java to PHP.
Requirements
- PHP 8.0+
Installation
composer require odan/tsid
Usage
use Odan\Tsid\TsidFactory; $tsidFactory = new TsidFactory(); $tsid = $tsidFactory->generate(); // 388400145978465528 echo $tsid->toInt(); // 0ARYZVZXW377R echo $tsid->toString();
Database Usage
MySQL
Use bigint(20) unsigned
as datatype for the (primary / secondary) key.
Example:
CREATE TABLE `users` ( `id` bigint(20) unsigned NOT NULL, `username` varchar(45) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB
Note: When you use BIGINT(20)
the maximum value is 2^63 - 1 == 9223372036854775807
.
This means there is still enough space to store any TSID.
When you use BIGINT(20) unsigned
the maximum value is: 2^64-1 = 18446744073709551615
SQLite
Use INTEGER
as datatype for the (primary / secondary) key.
CREATE TABLE users (id INTEGER PRIMARY KEY, username TEXT);
Note: SQLite uses an 8-byte signed integer to store integers.
So the maximum positive integer value is 2^63 - 1 == 9223372036854775807
.
This means there is still enough space to store any TSID.
Data Type Comparison
TSID max: 18446744073709551615
TSID 2023-01-01T00:00:00.000Z: 397177100698290050
TSID 2038-01-19T03:14:07.000Z: 2389272048961164191
TSID 2999-12-31T23:59:59.999Z: 7015104302283010234
PHP_INT_MAX: 9223372036854775807
SQLite INTEGER max: 9223372036854775807
MySQL BIGINT(20) max: 9223372036854775807
MySQL BIGINT(20) unsigned max: 18446744073709551615
Read more
- https://vladmihalcea.com/uuid-database-primary-key/
- https://github.com/f4b6a3/tsid-creator
- UUID version 7 features a time-ordered value field.
- ULID's are 128-bit numbers with a timestamp and random bits.
License
The MIT License (MIT). Please see License File for more information.