<?php
function uuid($c = 4) {
$x = function($c) {
for ($n = ''; strlen($n) < $c + 1; $n = $n.base_convert(mt_rand(), 10, 16));
return substr($n, -$c);
};
return
(microtime(true) * 10000).'-'.$x($c).'-'.$x($c).'-'.$x($c).'-'.$x($c).'-'.$x($c).'-'.$x($c)
;
return sprintf(
'%x-%s-%s-%s-%s%s%s',
microtime(true) * 10000, $x($c), $x($c), $x($c), $x($c), $x($c), $x($c)
);
}
$time = microtime();
$uuids = array();
for ($i = 0; $i < 100; $i++) {
$uuids[] = uuid();
}
if (count(array_unique($uuids)) != count($uuids)) {
echo 'DUPLICATED';
} else {
echo 'No duplicate';
print_r($uuids);
}
preferences:
34.68 ms | 402 KiB | 5 Q