Reputation: 2721
I have to generate unique ID depending on time, counter and random prepended value from 1 to 10. The way I generate it:
$time = (int)(time()+$i);
$time = $time.(rand(1,9));
//At this step we have strings looking like this:
"13480835672" //This is time in first iteration
"13480835672" //This is time in second iteration
//But if I convert it to int
$time = (int)$time;
2147483647 //This is time converted in first iteration
2147483647 //This is time converted in second iteration
As you can see above times are same. All of them. What Am I missing here?
Upvotes: 0
Views: 115
Reputation: 16107
2147483647 is the maximum (signed) integer number your operating system / php binary can work with.
2147483647*2 = 2^32 which means your operating system / php binary is working 32 bits.
In the above explanation I'm multiplying by two because the integers used by php are signed which means they span both on the negative and the positive axis of integer numbers.
Using a float value such as one returned by microtime(TRUE)
allows you to work with much bigger numbers.
Upvotes: 1
Reputation: 5399
Use microtime to get the exact time when the function occured.
You should also use uniqid to get a proper random return.
Upvotes: 0