Generate UUID v6

Generate a reordered time-based UUID v6 with improved sortability for databases.

Code

Utilities
$timeGreg = (int)(microtime(true) * 10000000) + 0x01b21dd213814000;
$timeHigh = ($timeGreg >> 28) & 0xffffffff;
$timeMid = ($timeGreg >> 12) & 0xffff;
$timeLow = ($timeGreg & 0xfff) | 0x6000;
$clockSeq = mt_rand(0, 0x3fff) | 0x8000;
$node = mt_rand(0, 0xffffff) << 24 | mt_rand(0, 0xffffff);
return sprintf('%08x-%04x-%04x-%04x-%012x', $timeHigh, $timeMid, $timeLow, $clockSeq, $node);
Server

More PHP Snippets