Generate a quote number?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mercuryinhell
Forum Newbie
Posts: 18
Joined: Wed Apr 19, 2006 1:24 pm

Generate a quote number?

Post 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.
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

I think this is what you're looking for: http://us3.php.net/microtime
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
mercuryinhell
Forum Newbie
Posts: 18
Joined: Wed Apr 19, 2006 1:24 pm

Awesome

Post 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.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post 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);
}
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post 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.
Post Reply