31000 years support
This commit is contained in:
@@ -1,21 +1,26 @@
|
||||
<?php
|
||||
|
||||
function toBase62($num) {
|
||||
$chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
|
||||
$result = "";
|
||||
$base62 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
|
||||
$encoded = "";
|
||||
while ($num > 0) {
|
||||
$result = $chars[$num % 62] . $result;
|
||||
$encoded = $base62[$num % 62] . $encoded;
|
||||
$num = floor($num / 62);
|
||||
}
|
||||
return $result;
|
||||
return $encoded;
|
||||
}
|
||||
|
||||
function generateID() {
|
||||
$epochStart = strtotime('2020-01-01') * 1000;
|
||||
$now = round(microtime(true) * 1000) - $epochStart;
|
||||
$random = rand(0, 999); // 3-digit random number
|
||||
$id = ($now * 1000) + $random; // Combine timestamp + randomness
|
||||
return substr(str_pad(toBase62($id), 8, "0", STR_PAD_LEFT), 0, 8); // Ensure 8 chars
|
||||
$epochStart = strtotime("2020-01-01") * 1000; // Milliseconds since 2020
|
||||
$now = round(microtime(true) * 1000);
|
||||
$timestamp = ($now - $epochStart) % 1000000000000; // Keep last 12 digits
|
||||
|
||||
$randomNum = rand(0, 99); // Random 2-digit number
|
||||
$finalNum = ($timestamp * 100) + $randomNum;
|
||||
|
||||
return str_pad(toBase62($finalNum), 8, "0", STR_PAD_LEFT);
|
||||
}
|
||||
|
||||
echo generateID(); // Example output: "4D7hF2L3"
|
||||
echo generateID(); // Example output: "A1b2C3D4"
|
||||
|
||||
?>
|
||||
|
||||
Reference in New Issue
Block a user