diff --git a/README.md b/README.md index 9317cde..07d7deb 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ Unique, lexicographically sortable identifiers. =============================================== -Create a `new Ulid()` anywhere in your application, and you have a stringable object that can be used as the primary key in a database. Ulid strings look something like `01G2J6MYN0PGC5Q21W9C`. They are cryptographically pseudo-random, and sort so that newer Ulids compare "greater than" older Ulids. +Create a `new Ulid()` anywhere in your application, and you have a stringable object that can be used as the primary key in a database. Ulid strings look something like `01G2J6MYN0PGC5Q21W9C` or can be prefixed with a type like `CUSTOMER_01G2J6MYN0PGC5Q21W9C`. They are cryptographically pseudo-random, and sort so that newer Ulids compare "greater than" older Ulids. This solves the problems exposed with working with auto-incrementing integer primary keys, which are predictable and difficult to work with in distributed databases. @@ -29,7 +29,7 @@ This solves the problems exposed with working with auto-incrementing integer pri use Gt\Ulid\Ulid; $exampleDataSource->create(new Person( - new Ulid(), + new Ulid("pet"), name: "Cody", age: 5, )); diff --git a/src/Ulid.php b/src/Ulid.php index 880d844..edd6c48 100644 --- a/src/Ulid.php +++ b/src/Ulid.php @@ -1,6 +1,7 @@ randomString; } + + public function getDateTime():DateTime { + $timestamp = $this->getTimestamp() / 1000; + return new DateTime("@" . $timestamp); + } + } diff --git a/test/phpunit/UlidTest.php b/test/phpunit/UlidTest.php index d880d15..199df2b 100644 --- a/test/phpunit/UlidTest.php +++ b/test/phpunit/UlidTest.php @@ -1,6 +1,7 @@ getTimestamp(), + $sut->getDateTime()->getTimestamp() + ); + } + + public function testGetDateTime_constructorInit():void { + $dateTimeString = "1988-05-04 17:24"; + $knownUlid = new Ulid(timestamp: strtotime($dateTimeString) * 1000); + + $sut = new Ulid(init: $knownUlid); + self::assertSame($dateTimeString, $sut->getDateTime()->format("Y-m-d H:i")); + } }