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.
Generate a quote number?
Moderator: General Moderators
I think this is what you're looking for: http://us3.php.net/microtime
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.
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
Thanks guys. I used:
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.
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();
?>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.
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.
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.