Skip to content

Instantly share code, notes, and snippets.

@Balamir
Created October 17, 2016 20:08
Show Gist options
  • Select an option

  • Save Balamir/2cd3cf4f33d6c15dfd163502b1e94ef2 to your computer and use it in GitHub Desktop.

Select an option

Save Balamir/2cd3cf4f33d6c15dfd163502b1e94ef2 to your computer and use it in GitHub Desktop.
Generate SQL Like GUID with PHP
<?php
function getGUID(){
if (function_exists('com_create_guid')){
return com_create_guid();
}else{
mt_srand((double)microtime()*10000);//optional for php 4.2.0 and up.
$charid = strtoupper(md5(uniqid(rand(), true)));
$hyphen = chr(45);// "-"
$uuid = chr(123)// "{"
.substr($charid, 0, 8).$hyphen
.substr($charid, 8, 4).$hyphen
.substr($charid,12, 4).$hyphen
.substr($charid,16, 4).$hyphen
.substr($charid,20,12)
.chr(125);// "}"
return $uuid;
}
}
$GUID = getGUID();
echo $GUID;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment