如何根据历史时间生成UUID?

时间:2018-03-03 01:11:46

标签: php uuid

我正在使用https://github.com/ramsey/uuid库生成UUID(基于时间的v1 UUID)。

如何使用历史时间而不是当前时间从此库生成UUID?

我觉得它与FixedTimeProvider类有关,如下所示,但不知道如何让它工作。

$uuidFactory = new \Ramsey\Uuid\UuidFactory();
$uuidFactory->setTimeGenerator(new \Ramsey\Uuid\Generator\DefaultTimeGenerator(
    new \MyVendor\Uuid\NodeProvider(),
    new \MyVendor\Uuid\TimeConverter(),
    new \MyVendor\Uuid\TimeProvider()
));
\Ramsey\Uuid\Uuid::setFactory($uuidFactory);

$uuid = \Ramsey\Uuid\Uuid::uuid1();

1 个答案:

答案 0 :(得分:0)

以下是您执行此操作的方法:

$timestamp = strtotime('2017-01-01 12:00:00');
$startTimeProvider = new FixedTimeProvider([
        'sec' => $timestamp,
        'usec' => 0,
    ]);
$startTimeFeatureSet = new FeatureSet();
$startTimeFeatureSet->setTimeProvider($startTimeProvider);
$factory = new UuidFactory($startTimeFeatureSet);
echo Uuid::uuid1()->toString();

您可以测试此处生成的UUID的时间戳:https://www.famkruithof.net/uuid/uuidgen