diff --git a/src/Function/Misc.php b/src/Function/Misc.php index 5436146..5db9055 100644 --- a/src/Function/Misc.php +++ b/src/Function/Misc.php @@ -6,7 +6,7 @@ Desc. : Fungsi Custom Lain Lain Created By : Siau Pheng (siaupheng@3fonia.com). Created Date : January 27th, 2008. - Last Modified : August 21st, 2026. + Last Modified : October 27th, 2026. (c) 3FONIA Software; WWW.3FONIA.COM. @@ -362,3 +362,41 @@ if (!function_exists('random_color')) { return "rgba(".$col_r.",".$col_g.",".$col_b.",".$trans.")"; } } + +if (!function_exists('gen_uuidv4')) { + function gen_uuidv4(): string { + $data = random_bytes(16); + // Set version to 0100 (UUID v4) + $data[6] = chr(ord($data[6]) & 0x0f | 0x40); + // Set bits 6-7 to 10 + $data[8] = chr(ord($data[8]) & 0x3f | 0x80); + return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4)); + } +} + +if (!function_exists('gen_uuidv7')) { + function gen_uuidv7(int $time=0): string { + // Get timestamp in milliseconds (48 bits) + if (empty($time)) $time = (int) floor(microtime(true) * 1000); + // Generate 16 bytes of random data + $randomBytes = random_bytes(16); + // Pack the 48-bit timestamp into the first 6 bytes + $timeHex = str_pad(dechex($time), 12, '0', STR_PAD_LEFT); + $bytes = hex2bin($timeHex); + // Append 10 bytes of randomness (we need 16 total bytes) + $bytes .= substr($randomBytes, 0, 10); + // Apply the version bits (Version 7 -> 0b0111) onto byte 6 + $bytes[6] = chr((ord($bytes[6]) & 0x0f) | 0x70); + // Apply the variant bits (Variant 1 -> 0b10xx) onto byte 8 + $bytes[8] = chr((ord($bytes[8]) & 0x3f) | 0x80); + // Unpack to hex string and format with hyphens + $hex = bin2hex($bytes); + return sprintf('%08s-%04s-%04s-%04s-%012s', + substr($hex, 0, 8), + substr($hex, 8, 4), + substr($hex, 12, 4), + substr($hex, 16, 4), + substr($hex, 20, 12) + ); + } +}