Sufyan

Function UUID v4

Aug 19th, 2016
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.71 KB | None | 0 0
  1.     function UUIDv4() {
  2.        return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
  3.  
  4.          // 32 bits for "time_low"
  5.          mt_rand(0, 0xffff), mt_rand(0, 0xffff),
  6.  
  7.          // 16 bits for "time_mid"
  8.          mt_rand(0, 0xffff),
  9.  
  10.          // 16 bits for "time_hi_and_version",
  11.          // four most significant bits holds version number 4
  12.          mt_rand(0, 0x0fff) | 0x4000,
  13.  
  14.          // 16 bits, 8 bits for "clk_seq_hi_res",
  15.          // 8 bits for "clk_seq_low",
  16.          // two most significant bits holds zero and one for variant DCE1.1
  17.          mt_rand(0, 0x3fff) | 0x8000,
  18.  
  19.          // 48 bits for "node"
  20.          mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff)
  21.        );
  22.      }
Add Comment
Please, Sign In to add comment