Created
January 16, 2024 05:00
-
-
Save vrdriver/ff136ea4e77346031aac2c29e773fc84 to your computer and use it in GitHub Desktop.
Create a random UUID in PHP based on the UUID V1 format
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Create a random UUID in PHP based on the UUID V1 format | |
Output example: e22e1622-5c14-11ea-b2f3-0242ac130003 | |
The MIT License (MIT) | |
Copyright (c) 2024 Stephen Monro | |
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
*/ | |
function generateUuidV1() { | |
// Get the current timestamp | |
$time = microtime(true) * 10000000 + 122192928000000000; | |
// Convert timestamp to 64-bit binary | |
$timeBin = pack('J', (int)floor($time)); | |
// Get the clock sequence (14 bits) | |
$clockSeq = random_int(0, 0x3fff); | |
$clockSeqHex = str_pad(dechex($clockSeq), 4, '0', STR_PAD_LEFT); | |
$clockSeqBin = hex2bin($clockSeqHex); | |
// Get the node (48 bits) | |
//$node = hex2bin('0242ac130003'); // Replace this with your desired node value | |
// Generate a random node (48 bits) | |
$node = random_bytes(6); | |
// Set the multicast bit and the least significant bit | |
$node[0] = chr(ord($node[0]) | 0x01); | |
// Format the UUID | |
$uuidBin = sprintf( | |
'%08s%04s1%03s%04s%012s', | |
bin2hex(substr($timeBin, 4)), // Time_low | |
bin2hex(substr($timeBin, 2, 2)), // Time_mid | |
bin2hex(substr($timeBin, 0, 2)), // Time_hi_and_version | |
$clockSeqHex, // Clock_seq_hi_and_reserved | |
bin2hex($node) // Node | |
); | |
// Convert binary UUID to string | |
$uuid = vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split($uuidBin, 4)); | |
return $uuid; | |
} | |
// Example use case | |
$generatedUuid = generateUuidV1(); | |
echo $generatedUuid . "\n"; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment