Skip to content

Instantly share code, notes, and snippets.

@globalideias
Last active January 14, 2025 21:29
Show Gist options
  • Save globalideias/7b484e4741ac6ca0a522903831c486e2 to your computer and use it in GitHub Desktop.
Save globalideias/7b484e4741ac6ca0a522903831c486e2 to your computer and use it in GitHub Desktop.
Cria um número um código único com 12 caracteres que pode ser utilizado em voucher ou cupom.
function code_unique_12($with_sep = true)
{
// $letras = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'X', 'Y', 'Z'];
$letras = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
$numeros = time();
// $numeros = 99999999999;
$partes = [];
$length = 1;
for ($i = 0; $i < strlen($numeros); $i++) {
$partes[$i] = substr($numeros, $i, $length);
}
for ($j = $i; $j < 12; $j++) {
$partes[$j] = $letras[rand(0, 24)];
}
$cadeia = [];
$max_cadeia = 3;
$lenght_char_cadeira = count($partes) / $max_cadeia; // 5
$str_partes = join($partes);
for ($x = 0; $x < $max_cadeia; $x++) {
$cadeia[$x] = substr($str_partes, ($x * $lenght_char_cadeira), $lenght_char_cadeira);
}
if ($with_sep) {
return join("-", $cadeia);
}
return join("", $cadeia);
}
// Saida exemplo: 1736-8896-12LI
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment