There seem to be a few php functions to generate random uuids, but I couldn't find anything to generate something like the time based uuid which is version 1 of the rfc 4122 specification http://www.ietf.org/rfc/rfc4122.txt Here's my quick and dirty attempt at a function. It generates a random number for the clock sequence masked with the reserved bits, but that could be replaced with an actual clock sequence if you have something available. It uses the string_base_convert function to convert from decimal to binary and hex. For the large numbers required for the number of 100 nanosecond intervals since the adoption of the gregorian calender on 15 October 1582 I concatenate part of the microsecond substring. If I don't have the correct offset for the unix epoch, feel free to contact me and let me know (contact link on home page).

The last portion of the UUID is the machine's MAC address, which you may not have access to (or may not wish to reveal), or might not be able to use php to execute ifconfig, etc. so you can put the mac address in as a string, or use a random hex string for the last part. (There may be issues if you're trying to follow the RFC, some specific bits are expected for the different versions of UUID.)

Try out the code to generate a uuid here.

__________
<?php
function string_base_convert ($numstring, $frombase, $tobase) {

$chars = "0123456789abcdefghijklmnopqrstuvwxyz";
$tostring = substr($chars, 0, $tobase);

$length = strlen($numstring);
$result = '';
for ($i = 0; $i < $length; $i++) {
	$number[$i] = strpos($chars, $numstring{$i});
}
do {
	$divide = 0;
	$newlen = 0;
	for ($i = 0; $i < $length; $i++) {
		$divide = $divide * $frombase + $number[$i];
		if ($divide >= $tobase) {
			$number[$newlen++] = (int)($divide / $tobase);
			$divide = $divide % $tobase;
		} elseif ($newlen > 0) {
			$number[$newlen++] = 0;
		}
	}
	$length = $newlen;
	$result = $tostring{$divide} . $result;
}
while ($newlen != 0);
return $result;
}

function generate_uuid () {

$version = "0001";
$offset = 12219292800;
list($usec, $sec) = explode(" ", microtime());;
$gregorianseconds = $sec + $offset;
$nano100s = substr($usec, 2, 7);
$gregorian = $gregorianseconds . $nano100s;
$bin = string_base_convert ($gregorian,10,2);
$binpad =  str_pad($bin, 60, "0", STR_PAD_LEFT);
$clockseq = (mt_rand( 0, 0x3fff ) | 0x8000);

#random clock seq
$clockseqhex = str_pad(string_base_convert("$clockseq",10,16), 4, "0", STR_PAD_LEFT);

$time_low = (substr($binpad, -32));
$time_low_hex = str_pad(string_base_convert ($time_low,2,16), 8, "0", STR_PAD_LEFT);

$time_mid = (substr($binpad, -48, 16));
$time_mid_hex = str_pad(string_base_convert ($time_mid,2,16), 4, "0", STR_PAD_LEFT);

$time_hi = (substr($binpad, 0, 12));
$time_hi_and_version = $version . $time_hi;
$time_hi_and_version_hex = str_pad(string_base_convert ($time_hi_and_version,2,16), 4, "0", STR_PAD_LEFT);

#find mac address, can be replaced with string or use random hex string
$command = "ifconfig | grep ether | awk {'print \$2'} | head -n 1";
$mac = str_replace(":", "", exec($command));


$components = array(
$time_low_hex,
$time_mid_hex,
$time_hi_and_version_hex,
$clockseqhex,
$mac,
);

$uuid = strtoupper(implode("-", $components));

return $uuid;
}

echo generate_uuid();
?>

up home