Page 1 of 1

Generate a quote number?

Posted: Fri Apr 21, 2006 12:41 pm
by mercuryinhell
Guys,
What do you think the best way would be to generate a quote so that there are no duplicates, and the number is not too long ( i already started using the session number )?

Right now it is currently session, but in the past (In ASP) I used

<%
DIM QUOTENUM
QUOTENUM = CLng(Now * Now)
%>
#<%=QUOTENUM%>

So it takes the Date/Time multiplied by the Date/Time, then converts it to an easy to use number.
So I would never get a duplicate, because as the seconds change, so did the number.

Is there some code in PHP that would do the same thing?
Or even something better I am unaware of?

Any help is greatly appreciated.
Thanks.

Posted: Fri Apr 21, 2006 1:25 pm
by hawleyjr
I think this is what you're looking for: http://us3.php.net/microtime

Posted: Fri Apr 21, 2006 1:50 pm
by pickle
Keep in mind that theoretically there is no way to ensure two random numbers aren't the same without double-checking the 2nd number against the 1st. If you want to be 100% sure that you won't have duplicates, you're going to have to store all previously generated random numbers, then double check every newly created one against the previous ones.

Granted, using microtime() will almost certainly give you unique numbers, it's not 100% guaranteed.

Awesome

Posted: Fri Apr 21, 2006 2:02 pm
by mercuryinhell
Thanks guys. I used:

Code: Select all

<?php
function uniqueTimeStamp() {
  $milliseconds = microtime();
  $timestring = explode(" ", $milliseconds);
  $sg = $timestring[1];
  $mlsg = substr($timestring[0], 2, 4);
  $timestamp = $sg.$mlsg;
  return $timestamp; 
}

echo uniqueTimeStamp();
?>
That works great. Although the number is a little longer than I wanted. I tried cutting down the digits, but had no luck, oh well. Works for me, lol.

Posted: Fri Apr 21, 2006 2:11 pm
by pickle
Looks good, but could be shorter ;)

Code: Select all

<?PHP
function uniqueTimeStamp()
{
  list($mlsg,$sg) = explode(' ',microtime());
  return(substr($mlsg,2,4).$sg);
}

Posted: Fri Apr 21, 2006 5:57 pm
by timvw
http://www.php.net/uniqid.

Btw, if you're storing the quotes in a db, most dbms have support for auto_increment/identity/sequences.. Which implement a failsafe way to generate 'unique' IDs.