Random 16Bit UUID - PHP Online

Form of PHP Sandbox

Enter Your PHP code here for testing/debugging in the Online PHP Sandbox. As in the usual PHP files, you can also add HTML, but do not forget to add the tag <?php in the places where the PHP script should be executed.



Your result can be seen below.

Result of php executing





Full code of Random 16Bit UUID.php

  1. <?php
  2. $hex = bin2hex(random_bytes(16));
  3. $timeHi = mb_substr($hex, 12, 4);
  4. $timeHi = hexdec($timeHi) & 0x0fff;
  5. $timeHi &= ~0xf000;
  6. $timeHi |= 4 << 12;
  7. $clockSeqHi = hexdec(mb_substr($hex, 16, 2));
  8. $clockSeqHi &= 0x3f;
  9. $clockSeqHi &= ~0xc0;
  10. $clockSeqHi |= 0x80;
  11.  
  12.  
  13. $id = sprintf(
  14.     '%08s%04s%04s%02s%02s%012s',
  15.     // time low
  16.     mb_substr($hex, 0, 8),
  17.     // time mid
  18.     mb_substr($hex, 8, 4),
  19.     // time high and version
  20.     str_pad(dechex($timeHi), 4, '0', \STR_PAD_LEFT),
  21.     // clk_seq_hi_res
  22.     str_pad(dechex($clockSeqHi), 2, '0', \STR_PAD_LEFT),
  23.     // clock_seq_low
  24.     mb_substr($hex, 18, 2),
  25.     // node
  26.     mb_substr($hex, 20, 12)
  27. );
  28.  
  29. echo $id;
File Description
  • Random 16Bit UUID
  • PHP Code
  • 22 Aug-2022
  • 659 Bytes
You can Share it: