0

我必须根据时间、计数器和从 1 到 10 的随机前置值生成唯一 ID。我生成它的方式:

$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

正如你所看到的,上面的时间是一样的。他们都是。我在这里想念什么?

4

2 回答 2

1

2147483647 是您的操作系统/php 二进制文件可以使用的最大(有符号)整数。

2147483647*2 = 2^32 这意味着您的操作系统 / php 二进制文件正在 32 位运行。

在上面的解释中,我乘以 2,因为 php 使用的整数是有符号的,这意味着它们跨越整数的负轴和正轴。

使用诸如返回的浮点值microtime(TRUE)允许您处理更大的数字。

于 2012-09-19T19:51:03.813 回答
0

使用microtime获取函数发生的确切时间。

您还应该使用uniqid来获得适当的随机返回。

于 2012-09-19T19:47:57.667 回答