我正在使用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();
答案 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