Skip to content

Instantly share code, notes, and snippets.

@forecho
Last active December 25, 2015 08:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save forecho/ed2e61a4d75991fa0df9 to your computer and use it in GitHub Desktop.
Save forecho/ed2e61a4d75991fa0df9 to your computer and use it in GitHub Desktop.
Primary Id
<?php
static $OUR_EPOCH = 1444647842959;
static $DEFAULT_SHARD_ID = 1;
/**
* 生成 bigint64 位主键
* @return int
*/
public static function generatePk()
{
$microTime = intval(microtime(true) * 1000);
$id = ($microTime - static::$OUR_EPOCH) << (64-41);
$id |= (static::$DEFAULT_SHARD_ID << (64-41-13));
$id |= (mt_rand(1, 99999999) % 1024);
return $id;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment