colyll/date-snowflake

Add date string before a Short twitter's snowflake ID

dev-main 2021-04-21 03:31 UTC

This package is auto-updated.

Last update: 2024-09-08 14:46:06 UTC


README

Add date string before a Short twitter's snowflake ID, e.g. 2021031336140492049070.

/**
 *  由于雪花算法加上日期有26位长,所以修改缩小到22位。
 * |-------------------------雪花算法 (64bits)-------------------------------|
 * |--补位(1bit)--|---时间戳毫秒(41bit)---|---机器ID(10bit)--|---序号(12bit)--|
 *
 *           |-----------------------修改后(48bits)---------------------------|
 *    日期 + |----每日当前毫秒(28bit)---|---机器ID(9bit)---|----序号(11bit)----|
 *
 */

use

a. General usage

require('../src/DateSnowflake.php');
use Colyll\DateSnowflake;
$dateSnowflake = new DateSnowflake(10);

$id = $dateSnowflake->id();

b. Use it in Laravel project

composer require colyll/date-snowflake dev-laravel

copy config/snowflake.php into project_root/config/ directory, use SNOWFLAKE_MACHINE_ID=1 to set the machine ID in .envfile.

use Colyll\DateSnowflake;
$dateSnowflake = new DateSnowflake();

$id = $dateSnowflake->id();